bis*_*anu 11 android android-intent android-layout android-listview
我正在尝试创建一个带有6个按钮的布局,这些按钮可以自动适应屏幕尺寸,就像Windows手机的瓷砖一样.在代码中我动态创建了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>
Run Code Online (Sandbox Code Playgroud)
Voi*_*icu 18
我会使用一个垂直LinearLayout的三排相同重量的儿童,每排是一个水平,LinearLayout有两个相同重量的孩子,这将确保填满整个区域.对于六个按钮,性能应该不是问题.
如果需要考虑性能,可以将行设为RelativeLayouts并使用支柱将其分成两半并根据该位置定位两个子项.
当我说一个支柱时,我的意思是:
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
Run Code Online (Sandbox Code Playgroud)
更新:
既然你正在尝试LinearLayouts,那么你可以在这里处理高度和宽度:
父母LinearLayout可以:
android:layout_width="match_parent"
android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)
这三个LinearLayout孩子将:
android:layout_width="match_parent"
android:layout_height="0dip"
Run Code Online (Sandbox Code Playgroud)
该Button旨意有:
android:layout_width="0dip"
android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)
正如您所注意到的那样,0dip对于属性应用权重(如果父级是垂直方向,则为高度,如果父级是水平方向,则为宽度),需要增长以填充空间.
这是完整的XML(按钮不包括drawables,所以随意添加你的):
<?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>
Run Code Online (Sandbox Code Playgroud)
结果如下:

| 归档时间: |
|
| 查看次数: |
27310 次 |
| 最近记录: |