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

时间:2013-05-11 13:47:36

标签: android eclipse android-layout adt

如果我有很多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. 有什么理由说明为什么要报告此警告?

5 个答案:

答案 0 :(得分:10)

您是否尝试过清理该项目?

我在一些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>

答案 1 :(得分:3)

Build > Clean Project 尝试后,我仍然在布局编辑器中遇到同样的问题;我的IDE完全重启解决了我的问题。

答案 2 :(得分:1)

我试过这个,它的工作正常......

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

答案 3 :(得分:0)

旧话题,但仍然:)

我认为为样式设置宽度和高度不是一个好主意。基本上,它可以编译并可以正常工作,但是我遇到的情况是布局更加复杂,包括其他引起问题的地方。而且,当您考虑它时,将其放置在那里并没有任何意义。

如果要为不同的布局使用不同的值,可以轻松使用@dimen。对于使用您的样式的其他人来说,这也可能非常令人惊讶。通常,您希望通过样式设置颜色,大小和行为,但是大小不是其中之一。

您永远都不知道您的风格将在什么情况下使用。这只是这些令人讨厌的事情之一,当您看到自己开始感到有些事情本可以做得更好的时候。

答案 4 :(得分:-3)

我认为你应该把标签的父母作为&#34; @android:style / Widget.TextView&#34;