Android 从自定义主题继承所需的XML属性 问题是:

Android 从自定义主题继承所需的XML属性 问题是:,android,xml,android-layout,android-widget,Android,Xml,Android Layout,Android Widget,我有几个文本视图,它们共享大量相同的属性。我想将以下样式应用于所有文本视图 <!-- TextView --> <style name="fieldLabel" parent="@android:style/Widget.TextView"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wr

我有几个文本视图,它们共享大量相同的属性。我想将以下样式应用于所有文本视图

<!-- 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>
只有当我尝试将此样式作为主题应用时,才会发生这种情况。因为当我加上

style="@style/fieldLabel"
对于每个TextView的属性,它都会按预期工作,因此错误来自尝试将此主题应用于整个活动


守则: 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>
<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>
<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>

@样式/字段标签
填补家长的空缺
包装内容
0.7
18便士
myactivity.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>
<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>
<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>

AndroidManifest.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>
<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>
<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>

,但我决定问一个新问题,而不是重提旧问题

总结如下:

<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>
<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>
<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>
我知道,只要简单地添加layout_width和layout_height属性,或者向每个TextView显式添加style=“@style/fieldLabel”,就可以轻松完成这项工作。但这个问题的重点更多的是为了改进我(希望还有其他人)的编码约定,以及使代码本身更具可读性和可重用性

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

谢谢您的帮助。

这就是问题所在:

原因:java.lang.RuntimeException:二进制XML文件行#20:您 必须提供布局宽度属性


您的文本视图id名称标签需要一个
android:layout\u height
android:layout\u width
属性。

这通常是一个与布局相关的异常,您可以发布完整的堆栈跟踪和布局文件吗。@Dan我已经编辑了我的原始问题,以包括完整的堆栈跟踪和整个布局文件。谢谢你的帮助!是的,但我提供了一个布局高度和一个布局宽度,使用一个自定义主题,应该应用于活动中的所有文本视图。所以问题是,为什么它们不能正确继承这些字段?我从未见过有人通过样式应用高度和宽度。每个TextView是否需要自己的style属性?如果我显式地将style属性添加到每个TextView中,它会工作,但我希望通过使用清单中的android:theme属性将样式添加到整个活动中来避免这种情况。我看不出这有什么理由不起作用,因此我的原始问题。顺便谢谢你的帮助!