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.
<!-- 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.
The Code:
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:
<!-- This is an example of one of the TextViews -->
<TextView
android:id="@+id/name_label"
android:text="@string/name_label"
/>
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.
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.