Why shouldn't I set layout_width and layout_height in a style?

别说谁变了你拦得住时间么 提交于 2019-11-30 10:52:37

Have you tried to clean the project?

I am using also the same thing on some XML files for the width and the length. Here is an example working on my app, you can have a look:

<TextView
    android:id="@+id/viewdescription"
    android:textIsSelectable="true"
    android:layout_below="@+id/rating_bar_view"
    android:minLines="8"
    style="@style/description" />

And the XML:

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

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.

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!