我正在尝试创建一个带有 6 个按钮的布局,这些按钮可以像 Windows Phone 的磁贴一样自动适应屏幕尺寸。在代码中,我动态创建了 6 个按钮,2 个用于行,但按钮应适合填充后者的屏幕大小。我该如何继续?
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
最佳答案
我会使用一个垂直的 LinearLayout
和三行相同权重的 child ,每行是一个水平的 LinearLayout
有两个相同权重的 child ,这将确保整个区域都被填满了。对于六个按钮,性能应该不是问题。
如果性能是一个问题,您可以将行设置为 RelativeLayout
并使用支柱将其分成两半并基于此定位两个子项。
当我说 strut 时,我的意思是:
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
更新:
由于您正在尝试使用 LinearLayout
,下面是处理高度和宽度的方法:
父 LinearLayout
可以有:
android:layout_width="match_parent"
android:layout_height="match_parent"
三个 LinearLayout
child 将有:
android:layout_width="match_parent"
android:layout_height="0dip"
Button
将具有:
android:layout_width="0dip"
android:layout_height="match_parent"
如您所见,我们为应用权重的属性设置了 0dip
(如果父级是垂直方向,则在高度上,如果父级是水平方向,则在宽度上),它需要增长填补空白。
这是完整的 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
结果:
关于android - 如何使用 6 个按钮创建布局,如 Windows 磁贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466914/