Note that there are some explanatory texts on larger screens.

plurals
  1. POLayout params of loaded view are ignored
    primarykey
    data
    text
    <p>I'm trying to write my own custom <code>View</code> and I have problem with <code>LayoutParams</code>. </p> <p>The idea is to extend <code>ViewGroup</code> (<code>LinearLayout</code>)</p> <pre><code>public class MyView extends LinearLayout{ public MyView(Context context, AttributeSet attrs) { super(context, attrs); } public MyView(Context context) { super(context); } public void putContent(){ setOrientation(HORIZONTAL); LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (int i = 0; i &lt; 5; i++){ View view = inflater.inflate(R.layout.item, null); TextView tv = (TextView)view.findViewById(R.id.item_text); tv.setText("Item " + i); addView(view); } } } </code></pre> <p>As you can see <code>putContent</code> method inflates an items and adds to my view. Here is an item layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"&gt; &lt;TextView android:text="TextView" android:id="@+id/item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>And main screen layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"&gt; &lt;TextView android:layout_width="match_parent" android:layout_height="wrap_content" android_layout_weight="1" android:text="@string/hello" /&gt; &lt;my.test.MyView android:id="@+id/my_view" android:layout_width="match_parent" android:layout_height="match_parent" android_layout_weight="1" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>And activity code</p> <pre><code>public class Start extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyView myView = (MyView)findViewById(R.id.my_view); myView.putContent(); } } </code></pre> <p>Here is screenshot of what i get </p> <p><img src="https://i.stack.imgur.com/EG5Of.png" alt="enter image description here"></p> <p>So problem is: attributes of item's root element is ignored</p> <pre><code>android:layout_width="match_parent" android:layout_height="match_parent" </code></pre> <p>But in result I want to get something like this (I get this result when replace <code>addView(view);</code> with this line)</p> <pre><code>addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F)); </code></pre> <p><img src="https://i.stack.imgur.com/3QCQj.png" alt="enter image description here"></p> <p>So question is: how I can achieve this result without hard coded LayoutParams ? Thanks for any help!</p> <p><strong>update</strong></p> <p>I also look at the <code>view</code> variable fields in debug mode - the <code>mLayoutParams</code> is <code>null</code>, and became not null when I add inflated <code>view</code> into parent with <code>addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));</code>. <img src="https://i.stack.imgur.com/vY4nU.png" alt="enter image description here"> </p> <p>But <code>mLayoutParams</code> of child of just-loaded view is not null. Why LayoutParams of only root element in xml layout are ignored when view inflated?</p>
    singulars
    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