android:如何平均划分线性布局元素

时间:2012-05-19 17:56:20

标签: android android-layout

我有一个线性布局,有两个列表视图和两个死按钮。如何在列表视图之间平均划分空间。我得到了一些建议,比如将高度设置为0dip,但它没有用。 eclipse中的图形布局显示了正确的布局,但是当我向列表添加不同数量的元素时,它们会扩展到不同的高度。这是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>

感谢

因为没有人相信我这里是我所得到的形象。 只是链接,因为我没有足够的声誉发布图片:( ! runeclipse

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

毕竟这可能不是基于此答案Linear Layout and weight in Android

的xml问题

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

这对于静态XML布局是正确的。如果您在运行时动态添加视图,则需要将addView与布局参数一起使用,例如addView(button,new LinearLayout.LayoutParams(0,height,1));即使您使用正确的宽度和重量值对布局进行充气,也是如此。 - Nuthatch 2011年9月3日21:41

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

我确实使用布局inflater将我的视图添加到选项卡。也许这就是问题.. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

以下是使用充气机时再现问题的示例代码。

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

又一次更新。 我认为这个问题就像Nuthatch(我从旧帖子中提到的那个人)所指出的那样:addView()只是弄乱了静态xml中的任何布局,这是一个很有价值的教训。当我将ParticipantsPanel中的代码移动到主活动,并直接设置视图而不将其添加到另一个linearlayout时,它按预期工作。我理解为什么会出现这种情况,以及如何将我的代码放在一个类中并且有所期望的行为。

2 个答案:

答案 0 :(得分:1)

您发布的代码不是一个好例子。当您使用ListViews时,两个LayoutInflater不占用相等空间的问题是因为您在不提供父ViewGroup的情况下使布局文件膨胀,从中LayoutParams }}。

如果您使用:

addView(myView, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

修改R.layout.participants并将父LinearLayout替换为merge标记并设置:

        this.setOrientation(LinearLayout.VERTICAL);
        this.setWeightSum(2.0f);
        li.inflate(R.layout.participants, this, true);

代表ParticipantsPanel

那么一切都应该没问题。

答案 1 :(得分:0)

对于垂直方向,将加权视图的高度设置为0px(或0dp,或者更确切地说,0在android上的所有单位中都是0)。 对于水平方向,请执行相同但宽度。

相关问题