Skip to content Skip to sidebar Skip to footer

Why Shouldn't I Set Layout_width And Layout_height In A Style?

If I have a lot of TextViews serving as item labels, I thought I could extract everything that's common about them into a style, and in the layout use only Copy

And the XML:

<stylename="description"><itemname="android:layout_gravity">center_horizontal</item><itemname="android:inputType">textMultiLine</item><itemname="android:layout_width">fill_parent</item><itemname="android:layout_height">wrap_content</item><itemname="android:textSize">14sp</item><itemname="android:textColor">@color/Black</item><itemname="android:layout_marginRight">8dp</item><itemname="android:layout_marginLeft">8dp</item>

Solution 2:

Old topic but still :)

I don't think setting a width and height to style is a good idea. Basically it compiles and usually works but I experienced situations with more complex layouts, includes etc. where it was causing problems. And when you think about it, it doesn't really make sense to put it there.

You can easily use @dimen if you want different values for different layouts. And it can also be pretty surprising for somebody else using your style. You usually want to set color, size, and behavior with style but size is not one of the things.

You never know in what context your style gonna be used in. It's just one of these smelly things which when you see you start to have a feeling that something could have been done better.

Solution 3:

After a Build > Clean Project attempt, I was still experiencing this same issue in the layout editor; a full restart of my IDE solved the problem for me.

Solution 4:

I have tried this, its just working fine..

 <EditText
        android:id="@+id/edt_mobile_no"
        style="@style/login_input_fields"
        android:hint="@string/hint_mobile"
        android:inputType="phone" />

Here below the style for the EditText

<stylename="login_input_fields"><itemname="android:layout_width">match_parent</item><itemname="android:layout_height">40dp</item><itemname="android:layout_marginBottom">10dp</item><itemname="android:background">@drawable/bg_curved_white_border</item><itemname="android:paddingRight">10dp</item><itemname="android:paddingLeft">10dp</item><itemname="android:textColor">@android:color/white</item><itemname="android:textColorHint">@color/underline</item><itemname="android:textSize">16sp</item><itemname="android:typeface">monospace</item></style>

Solution 5:

I thing you should put the parent of label as "@android:style/Widget.TextView"

Post a Comment for "Why Shouldn't I Set Layout_width And Layout_height In A Style?"