Note that there are some explanatory texts on larger screens.

plurals
  1. POInheriting Required XML Attributes from Custom Theme
    primarykey
    data
    text
    <h2>The Problem:</h2> <p>I have several TextViews that share a large number of identical attributes. I'd like to apply the following style to all the TextViews.</p> <pre><code>&lt;!-- TextView --&gt; &lt;style name="fieldLabel" parent="@android:style/Widget.TextView"&gt; &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt; &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt; &lt;item name="android:layout_weight"&gt;0.7&lt;/item&gt; &lt;item name="android:textSize"&gt;18sp&lt;/item&gt; &lt;/style&gt; </code></pre> <p>But for some reason, when I apply this style as an activity-wide theme I get this error:</p> <pre><code>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. </code></pre> <p>This is <em>only</em> happens when I try to apply this style as a theme. Because, when I add</p> <pre><code>style="@style/fieldLabel" </code></pre> <p>individually to every TextView's attributes it works as expected, so the error is coming from trying to apply this theme to the <em>entire activity.</em></p> <hr> <h2>The Code:</h2> <p><strong>styles.xml:</strong></p> <pre><code>&lt;style name="CustomActivityStyle" parent="android:Theme"&gt; &lt;!--This is theme I am trying to apply --&gt; &lt;item name="android:textViewStyle"&gt;@style/fieldLabel&lt;/item&gt; &lt;/style&gt; &lt;!-- TextView --&gt; &lt;style name="fieldLabel" parent="@android:style/Widget.TextView"&gt; &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt; &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt; &lt;item name="android:layout_weight"&gt;0.7&lt;/item&gt; &lt;item name="android:textSize"&gt;18sp&lt;/item&gt; &lt;/style&gt; </code></pre> <p><strong>myactivity.xml:</strong></p> <pre><code>&lt;!-- This is an example of one of the TextViews --&gt; &lt;TextView android:id="@+id/name_label" android:text="@string/name_label" /&gt; </code></pre> <p><strong>AndroidManifest.xml:</strong></p> <pre><code>&lt;!-- Implementing the theme in the Manifest --&gt; &lt;activity android:name=".MyActivity" android:configChanges="orientation" android:theme="@style/CustomActivityStyle" android:label="@string/my_title"&gt; &lt;/activity&gt; </code></pre> <p>There is another thread on this issue <a href="https://stackoverflow.com/questions/5642597/must-supply-a-layout-height-but-its-in-my-style-resources">here</a>, but I decided to ask a new question instead of reviving the old one.</p> <p><strong>To Summarize:</strong></p> <p>I do know that I could easily make this work by simply adding the layout_width and layout_height attributes, <em>or</em> 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.</p> <p>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.</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload