Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid: how todivide linear layout elements equally
    text
    copied!<p>I have a linear layout with two listviews and two dead buttons. How to divide the space equally between the list views. I got some suggestions like setting the height to 0dip but it didn't work. The graphical layout in eclipse shows a correct layout but when I add different number of elements to the lists they expand to different height. Here is the xml.</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" android:weightSum="2"&gt; &lt;Button android:layout_width="match_parent" android:layout_height="wrap_content" android:enabled="false" android:text="Installed engines" &gt; &lt;/Button&gt; &lt;ListView android:id="@+id/primary" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" &gt; &lt;/ListView&gt; &lt;Button android:layout_width="match_parent" android:layout_height="wrap_content" android:enabled="false" android:text="Active engines" &gt; &lt;/Button&gt; &lt;ListView android:id="@+id/secondary" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <h2>thanks</h2> <p>Since no one believes me here is an image of what I am getting. Just the links since I don't have enough reputation to post pics :( ! <a href="http://dl.dropbox.com/u/55295461/pic1.jpg" rel="nofollow noreferrer">run</a> ! <a href="http://dl.dropbox.com/u/55295461/pic2.jpg" rel="nofollow noreferrer">eclipse</a></p> <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p> <p>After all this may not be xml issue based on this answer <a href="https://stackoverflow.com/questions/2698817/linear-layout-and-weight-in-android">Linear Layout and weight in Android</a></p> <p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p> <p><em>This is correct for static XML layouts. If you're adding Views dynamically at runtime, you'll need to use addView with layout parameters like addView(button, new LinearLayout.LayoutParams(0, height, 1)); This is true even if you're inflating layouts with the correct width and weight values. – Nuthatch Sep 3 '11 at 21:41</em></p> <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p> <p>I do use the layout inflater to add my view to a tab. Maybe that is the problem .. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p> <p>Here is sample code that reproduces the problem when I use an inflator.</p> <pre><code>public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ParticipantsPanel p = new ParticipantsPanel(this); setContentView(p); } } class ParticipantsPanel extends LinearLayout { public ParticipantsPanel(Context ctx) { super(ctx); LayoutInflater li = LayoutInflater.from(ctx); View myView = li.inflate(R.layout.participants, null); final ListView primary = (ListView) myView.findViewById(R.id.primary); final ListView secondary = (ListView) myView.findViewById(R.id.secondary); final ArrayAdapter&lt;String&gt; primaryA = new ArrayAdapter&lt;String&gt;(ctx,R.layout.lvtext); final ArrayAdapter&lt;String&gt; secondaryA = new ArrayAdapter&lt;String&gt;(ctx,R.layout.lvtext); primaryA.add("hello1"); primaryA.add("hello2"); primaryA.add("hello3"); primaryA.add("hello4"); primaryA.add("hello5"); primaryA.add("hello6"); primaryA.add("hello7"); primaryA.add("hello8"); secondaryA.add("select1"); secondaryA.add("select2"); primary.setAdapter(primaryA); secondary.setAdapter(secondaryA); addView(myView); } } </code></pre> <p>xxxxxxxxxxxxxxxxxxxxxxx</p> <p>Yet another update. <strong>I think the problem is as Nuthatch ( the guy I referred to from an old post) pointed out : addView() just messes up whatever layout you have in static xml, which is a valuable lesson imo. When I move the code in the ParticipantsPanel to the main activity, and set the view directly without out adding it to another linearlayout, it works as expected. I appreciate an explanation as to why that is the case, and how I may still put my code in a class and have desired behavior.</strong></p>
 

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