如果我有许多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.
报告这个警告有什么原因吗?
发布于 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>
发布于 2018-08-20 19:34:38
老话题,但仍然是:)
我不认为设置样式的宽度和高度是一个好主意。基本上,它可以编译并正常工作,但我遇到了更复杂的布局、包含等情况,它造成了问题。当你思考它的时候,把它放在那里是没有意义的。
如果你想对不同的布局使用不同的值,你可以很容易地使用@dimen。对于其他使用你的风格的人来说,这也是非常令人惊讶的。您通常希望通过样式设置颜色、大小和行为,但大小不在其中。
你永远不会知道你的风格会被用在什么背景下。这只是其中一件难闻的事情,当你看到它的时候,你会开始有一种感觉,那就是有些事情本可以做得更好。
发布于 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>
https://stackoverflow.com/questions/16497758
复制相似问题