Reputation: 1299
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.
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Installed engines" >
</Button>
<ListView
android:id="@+id/primary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Active engines" >
</Button>
<ListView
android:id="@+id/secondary"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
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 :( ! run ! eclipse
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
After all this may not be xml issue based on this answer Linear Layout and weight in Android
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I do use the layout inflater to add my view to a tab. Maybe that is the problem .. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Here is sample code that reproduces the problem when I use an inflator.
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<String> primaryA =
new ArrayAdapter<String>(ctx,R.layout.lvtext);
final ArrayAdapter<String> secondaryA =
new ArrayAdapter<String>(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);
}
}
xxxxxxxxxxxxxxxxxxxxxxx
Yet another update. 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.
Upvotes: 0
Views: 1544
Reputation: 87064
That code you posted is not a good example. The problem of the two ListViews
that don't take equal space when you use the LayoutInflater
is because you inflate the layout file without supplying a parent ViewGroup
from which to take its LayoutParams
.
If you use:
addView(myView, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
or modify the R.layout.participants
and replace the parent LinearLayout
with a merge
tag and also setting:
this.setOrientation(LinearLayout.VERTICAL);
this.setWeightSum(2.0f);
li.inflate(R.layout.participants, this, true);
for the ParticipantsPanel
,
then all should be alright.
Upvotes: 1
Reputation: 116332
for vertical orientation , set the height of the weighted views to 0px (or 0dp , or whatver, 0 is a 0 in all units on android) . for horizontal orientation do the same but for width.
Upvotes: 0