我不明白如何使用这个属性。谁能多告诉我一点吗?
当前回答
结合以下两个答案
Flo, rptwsthi和roetzi,
记住要改变你的layout_width=0dp/px,否则layout_weight行为将会相反,最大的数字占据最小的空间,最小的数字占据最大的空间。
此外,一些权重组合会导致一些布局无法显示(因为它过度占用空间)。
当心这一点。
其他回答
结合以下两个答案
Flo, rptwsthi和roetzi,
记住要改变你的layout_width=0dp/px,否则layout_weight行为将会相反,最大的数字占据最小的空间,最小的数字占据最大的空间。
此外,一些权重组合会导致一些布局无法显示(因为它过度占用空间)。
当心这一点。
http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout
Layout_weight定义了控件相对于其他控件必须获得的空间大小。
使用layout_weight,你可以指定多个视图之间的大小比例。例如,你有一个MapView和一个表,它应该显示一些额外的信息到地图。地图应该使用屏幕的3/4,表格应该使用屏幕的1/4。然后将映射的layout_weight设置为3,将表的layout_weight设置为1。
为了让它工作,你还必须设置高度或宽度(取决于你的方向)为0px。
layout_weight tells Android how to distribute your Views in a LinearLayout. Android then first calculates the total proportion required for all Views that have a weight specified and places each View according to what fraction of the screen it has specified it needs. In the following example, Android sees that the TextViews have a layout_weight of 0 (this is the default) and the EditTexts have a layout_weight of 2 each, while the Button has a weight of 1. So Android allocates 'just enough' space to display tvUsername and tvPassword and then divides the remainder of the screen width into 5 equal parts, two of which are allocated to etUsername, two to etPassword and the last part to bLogin:
<LinearLayout android:orientation="horizontal" ...>
<TextView android:id="@+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content" ... />
<EditText android:id="@+id/etUsername"
android:layout_width="0dp"
android:layout_weight="2" ... />
<TextView android:id="@+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content" />
<EditText android:id="@+id/etPassword"
android:layout_width="0dp"
android:layout_weight="2" ... />
<Button android:id="@+id/bLogin"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Login"... />
</LinearLayout>
它看起来是这样的: 而且
对我来说最好的解释之一是这个(来自Android教程,请参阅第7步):
layout_weight is used in LinearLayouts to assign "importance" to Views within the layout. All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed. Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements. To give an example: let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important). If the first one has a layout_weight of 1 and the second has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?