Android layout_weight属性的工作原理
来源:互联网 发布:多个excel文件数据汇总 编辑:程序博客网 时间:2024/04/30 04:44
这篇文档主要讲一下layout_weight。给组件添加这个属性时,有一点需要注意,在Android studio中,Design中显示的布局会和手机上运行时显示的不一样,不要被Design蒙骗。
先上一段局部布局代码,和一张手机界面截图
局部代码:
<TextView
style="?android:listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_details_label"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="@+id/crime_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/crime_solved_label"
/>
</LinearLayout>
</LinearLayout>
手机界面截图:
从这些信息中,我们可以发现,当LinearLayout中的组件android:layout_width=”wrap_content”时,即便两个android:layout_weight都等于1,从手机上可以看到,比例并不一样,如果从Android studio 的Design上看呢?
Android studio Design:
大家可以发现,是一样的,到底哪个是对的呢?理论上来说是手机,下面我就要来讲解一下layout_weight属性的原理。
LinearLayout是分两个步骤来设置属性宽度的,第一步是查看layout_width的属性值,也就是说它会优先考虑layout_width的属性值,第二步是根据layout_weight属性值进行额外的空间分配,通俗点说,只要layout_width发话了,就全听layout_width的(其实也不是,layout_weight会有影响),如果layout_width没有发话,两个layout_width的值被设置为0dp,
布局按照第二步的设置来安排。
下面我们就来这么做,把layout_width设置成0dp,两个组件的layout_weight还都是1不变,上代码,Design图,手机界面截图。
局部代码:
<TextView
style="?android:listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_details_label"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="@+id/crime_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/crime_solved_label"
/>
</LinearLayout>
</LinearLayout>
Android studio Design:
当然,它还是保持不变。
手机界面截图:
通过这两个实验,我们知道它的属性设置是有优先级的。
- android:layout_weight属性的工作原理
- android:layout_weight属性的工作原理
- Android layout_weight属性的工作原理
- android:layout_weight 属性的工作原理
- Android xml中layout_weight属性的工作原理
- android:layout_weight属性的使用
- android:layout_weight属性的使用
- Android的layout_weight属性详解
- Android的layout_weight属性释疑
- Android布局的layout_weight属性
- android Layout_weight属性的详解
- 对android的layout_weight属性的理解
- 对android的layout_weight属性的理解
- android---android:layout_weight属性
- Android中你也许不知道的线性布局Layout_weight属性权重比例分配原理
- 详解 Android Views 元素的 layout_weight 属性
- Android布局属性Layout_weight的标准解释
- Android Views元素的layout_weight属性
- proxmark3
- 程序员必知之浮点数运算原理详解
- 神奇的Android Studio Template
- 第十四章 上机练习2......
- 【Medium】238. Product of Array Except Self
- Android layout_weight属性的工作原理
- JMockit Mockups API
- web安全 CSRF漏洞
- 项目应用:自定义异常处理
- Git的撤消操作 - 重置, 签出 和 撤消
- Android中webview与js交互
- app上架被拒的原因总结(转)
- Codeforces Round #371 (Div. 2)C. Sonya and Queries
- 自定义view的LayoutParam gravity layout_gravity