Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It has to do with the encouraged patterns within Android around using Views. This isn't the intended approach for what it looks like you're trying to do. First I'll explain what this mechanism is for and then suggest an approach for your app.</p> <p>The third argument to View constructors that takes an attr resource is generally used when implementing View subclasses and as you've shown, lets you specify a theme attribute to use as a reference to the View's default style. If you had a special kind of button called AwesomeButton you might implement its constructors like this:</p> <pre><code>public class AwesomeButton extends Button { public AwesomeButton(Context context) { this(context, null); } public AwesomeButton(Context context, AttributeSet attrs) { this(context, attrs, R.attr.awesomeButtonStyle); } public AwesomeButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr) { final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AwesomeButton, defStyleAttr, 0); // Read AwesomeButton-specific style attributes from a a.recycle(); } // More code } </code></pre> <p>When Android's <code>LayoutInflater</code> inflates views it uses the 2-argument constructor with the arguments <code>(Context, AttributeSet)</code>. The R.attr constant is passed through to the 3-argument version and then down to <code>Button</code>'s 3-argument constructor in the <code>super</code> call. This means that <code>Button</code> will read default styling info for the things it encapsulates from <code>AwesomeButton</code>'s default style as specified in your theme. Some Views within Android differ from their superclass only in the default style they use. (<code>Button</code> is actually one of these.)</p> <p>You specify <code>android:layout_width</code> and <code>android:layout_height</code> in your style but this can be problematic. <code>LayoutParams</code> (any attribute that starts with <code>layout_</code>) are specific to the parent view, not the view they appear on. This is why you always pass the intended parent view as the second parameter to <code>LayoutInflater#inflate</code> - it tells the inflater which class should be responsible for interpreting the <code>LayoutParams</code>. If you skip this you will often find that your <code>LayoutParams</code> don't behave as you expect and are often ignored outright. By convention we don't put <code>LayoutParams</code> in styles even though in some special cases it sort of works. </p> <p>It looks like you're trying to use a style as a sort of template. Is there a reason not to use a layout resource for this and specify the styling there?</p> <pre><code>final LayoutInflater inflater = LayoutInflater.from(mActivity); Button btn = (Button) inflater.inflate(R.layout.styled_button, parentView, false); </code></pre> <p>res/layout/styled_button.xml:</p> <pre><code>&lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/my_button_background" [...] /&gt; </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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