从自定义主题继承必需的XML属性

时间:2022-06-01 23:44:42

The Problem:

I have several TextViews that share a large number of identical attributes. I'd like to apply the following style to all the TextViews.

我有几个TextViews共享大量相同的属性。我想将以下样式应用于所有TextView。

<!-- TextView -->
<style name="fieldLabel" parent="@android:style/Widget.TextView">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">0.7</item>
    <item name="android:textSize">18sp</item>
</style>

But for some reason, when I apply this style as an activity-wide theme I get this error:

但由于某种原因,当我将此样式应用为活动范围的主题时,我收到此错误:

09-08 15:57:17.034: ERROR/AndroidRuntime(5269): Caused by: java.lang.RuntimeException: Binary XML file line #38: You must supply a layout_width attribute.

This is only happens when I try to apply this style as a theme. Because, when I add

只有当我尝试将此样式应用为主题时才会发生这种情况。因为,当我添加

style="@style/fieldLabel"

individually to every TextView's attributes it works as expected, so the error is coming from trying to apply this theme to the entire activity.

单独地对每个TextView的属性按预期工作,因此错误来自于尝试将此主题应用于整个活动。


The Code:

styles.xml:

styles.xml:

<style name="CustomActivityStyle" parent="android:Theme"> <!--This is theme I am trying to apply -->
    <item name="android:textViewStyle">@style/fieldLabel</item>
</style>

<!-- TextView -->
<style name="fieldLabel" parent="@android:style/Widget.TextView">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">0.7</item>
    <item name="android:textSize">18sp</item>
</style>

myactivity.xml:

myactivity.xml:

<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>

AndroidManifest.xml:

AndroidManifest.xml中:

<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>

There is another thread on this issue here, but I decided to ask a new question instead of reviving the old one.

这里有另一个关于这个问题的线索,但我决定提出一个新问题,而不是恢复旧问题。

To Summarize:

总结一下:

I do know that I could easily make this work by simply adding the layout_width and layout_height attributes, or by explicitly adding the style="@style/fieldLabel" to each TextView. But the point of this question is more to improve my (and hopefully others) coding conventions, as well as making the code itself more readable and reusable.

我知道我可以通过简单地添加layout_width和layout_height属性,或者通过向每个TextView显式添加style =“@ style / fieldLabel”来轻松完成这项工作。但是这个问题的重点更多的是改进我的(并希望是其他的)编码约定,以及使代码本身更具可读性和可重用性。

I am actually very surprised that no one has come across this issue before, and that this is not more of a standard way of formatting.

我真的很惊讶以前没有人遇到过这个问题,而且这不是一种标准的格式化方式。

Thanks for any help.

谢谢你的帮助。

1 个解决方案

#1


-1  

This is the problem:

这就是问题:

Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

引起:java.lang.RuntimeException:二进制XML文件行#20:您必须提供layout_width属性。

Your TextView id name_label, requires a android:layout_height and android:layout_width attribute.

你的TextView id name_label,需要一个android:layout_height和android:layout_width属性。

#1


-1  

This is the problem:

这就是问题:

Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

引起:java.lang.RuntimeException:二进制XML文件行#20:您必须提供layout_width属性。

Your TextView id name_label, requires a android:layout_height and android:layout_width attribute.

你的TextView id name_label,需要一个android:layout_height和android:layout_width属性。