首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么我不应该在样式中设置layout_width和layout_height?

为什么我不应该在样式中设置layout_width和layout_height?
EN

Stack Overflow用户
提问于 2013-05-11 21:47:36
回答 4查看 18.9K关注 0票数 32

如果我有许多TextViews作为项目标签,我认为我可以将它们的所有共同之处提取到样式中,并且仅在布局中使用

<TextView style="@style/label" android:text="Foo"/>
<TextView style="@style/label" android:text="Bar"/>

风格类似于:

<style name="label">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

但是当我这样做时,它在模拟器和设备上工作得很好,但是Android XML编辑器抱怨说,"<TextView>" does not set the required layout_height attribute.报告这个警告有什么原因吗?

EN

回答 4

Stack Overflow用户

发布于 2013-05-11 23:23:33

你有没有试过清理项目?

我在一些XML文件上也使用了相同的宽度和长度。这是一个在我的应用上工作的例子,你可以看看:

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

和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>

票数 10
EN

Stack Overflow用户

发布于 2018-08-20 19:34:38

老话题,但仍然是:)

我不认为设置样式的宽度和高度是一个好主意。基本上,它可以编译并正常工作,但我遇到了更复杂的布局、包含等情况,它造成了问题。当你思考它的时候,把它放在那里是没有意义的。

如果你想对不同的布局使用不同的值,你可以很容易地使用@dimen。对于其他使用你的风格的人来说,这也是非常令人惊讶的。您通常希望通过样式设置颜色、大小和行为,但大小不在其中。

你永远不会知道你的风格会被用在什么背景下。这只是其中一件难闻的事情,当你看到它的时候,你会开始有一种感觉,那就是有些事情本可以做得更好。

票数 3
EN

Stack Overflow用户

发布于 2016-01-13 16:05:21

我试过了,它工作得很好..

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

下面是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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16497758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档