Android在线性布局上的大小相等
好的,我想要以下布局: 我尝试的是使用带权重的线性布局。我真的不想在每个元素上加上硬编码的宽度以使它们正确。问题是我不能让那个大盒子和第一排的第二个盒子并排,我似乎不明白为什么。我可以用XML,使用权重或类似的东西来实现这一点吗 我目前的布局:Android在线性布局上的大小相等,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,好的,我想要以下布局: 我尝试的是使用带权重的线性布局。我真的不想在每个元素上加上硬编码的宽度以使它们正确。问题是我不能让那个大盒子和第一排的第二个盒子并排,我似乎不明白为什么。我可以用XML,使用权重或类似的东西来实现这一点吗 我目前的布局: <LinearLayout android:layout_width="match_parent" android:layout_height="55dp" android:layout_marginTop="15dp"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:weightSum="4" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="1" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center"
android:text="2" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center"
android:text="3" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center"
android:text="4" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="3"
android:text="Box x3 incl. margins" />
</LinearLayout>
这可能实现第五种线性布局,但。。。 使用权重,嵌套线性布局的成本很高。今天,我致力于优化非常复杂的布局,在高级设备上渲染只需14秒——在不改变外观的情况下进行更改后——不到1秒 你要找的可能是表格布局。这里是解决方案:
试试这个。。。
在这种布局中,第二行并非与第一行的列并排。但是很接近。
Try this...
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_weight="1"
android:text="button1" />
<Button
android:layout_weight="1"
android:text="button2" />
<Button
android:layout_weight="1"
android:text="button3" />
<Button
android:layout_weight="1"
android:text="button4" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:text="button5" />
<Button
android:layout_span="3"
android:text="button6" />
</TableRow>
</TableLayout>