Android布局分割

4

我希望将我的屏幕分成两个部分,我有一个LinearLayout,它又包含两个LinearLayouts

如何将这两个LinearLayouts平均分成两部分?


刚刚看了你对答案的回复。请告诉我,你想要如何分割它们?是垂直排列(上下堆叠)还是水平排列(并排出现)? - kishu27
4个回答

9

只需添加:

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"

对于它们两个,layout_weight 参数相等可以平均分配空间。

还要继续接受之前问题的一些答案。


你好,感谢您的解决方案,但是我无法将这两个布局分开,我已经硬编码了layout_width的值为200px,我不想这样做,请给出示例建议。 - ramsbh
这就是做法。如果你想硬编码你的宽度,那当然不会按比例缩放到50%。把它改成fill_parent。 - Kevin Coppock

1
通常我会使用android:layout_weight="1"来实现这个功能。你也可以使用表格布局。

0
我们可以在两个片段中使用android:layout_width="0dp"android:layout_weight="1",并且在它们的父级中,我们可以使用android:layout_width="fill_parent"

0

请检查此示例是否对您有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/rootlinearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:id="@+id/linearlayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <EditText
            android:id="@+id/et01"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="First TextBox"
            android:textSize="18sp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <EditText
            android:id="@+id/et02"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Another TextBox"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

嗨,谢谢回复。我想把屏幕垂直分成两部分,我已经使用了你的代码,但“AnotherTextbox”出现在“First Text box”之后,我想让第一个文本框占50%,第二个文本框占50%。有什么建议吗? - ramsbh
尝试在EditText中使用android:layout_height="match_parent"。这应该可以正常工作。 - Prashanth Babu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接