活动元素的宽度不正确?
问题描述:
我的应用程序的布局设置为480dpi宽,480/4是120.所以,如果我在应用程序顶部有4个按钮,并且我将其设置为120dpi宽,为什么按钮占用一半屏幕?!我怎样才能让按钮在顶部平均分配尺寸?活动元素的宽度不正确?
在这里,我只是增加了4个按键在顶部:
那么前两个按钮是120 dpi的宽:
答
检查了这一点:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
</LinearLayout>
答
可以使用FILL_PARENT参数进行布局宽度并为每个按钮添加一个权重1以获得期望的效果
+0
谢谢回答得这么快。所以每个按钮的宽度是“FILL_PARENT”,Activity的LinearLayout仍然是“match_parent”?你是什么意思“添加一个重量1”的按钮?当我这样做,它说我需要提供一个layout_width属性,我看不到按钮。 – Cole 2011-12-19 05:36:08
答
试试这个
<TableRow android:layout_width="fill_parent"
android:weightSum="100" android:layout_height="wrap_content">
<Button android:text="Button" android:layout_weight="25"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:layout_width="wrap_content"
android:layout_weight="25" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:layout_weight="25"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:layout_weight="25"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
发表您的布局xml文件代码.. – user370305 2011-12-19 05:28:58