Android UI杂谈---layout_weight
来源:互联网 发布:小学生机器人编程 编辑:程序博客网 时间:2024/05/24 05:09
对Android 中 LinearLayout中属性layout_weight讨论:
(一)
(1)当android:orientation=”horizontal”android:layout_width=”0dp”时:
<?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="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#C82D25" android:text="test activity1" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#EBF0F3" android:text="test activity2" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:background="#F2CACA" android:text="test activity3" /></LinearLayout>
表现:
(2)当
android:orientation=”horizontal”android:layout_width=”match_parent”时:
<?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="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#C82D25" android:text="test activity1" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:background="#EBF0F3" android:text="test activity2" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="#F2CACA" android:text="test activity3" /></LinearLayout>
表现:
(3)当
android:orientation=”horizontal”且 android:layout_width=”wrap_content”时:
<?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="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#C82D25" android:text="test activity1" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="#EBF0F3" android:text="test activity2" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="#F2CACA" android:text="test activity3" /></LinearLayout>
表现:
总结:
表现最佳的是第一种,第二种把第三个TextView挤掉了,第三种没有按比例排列,完全失去了layout_weight属性的作用。
从源码角度分析:
google LinearLayout源码:
if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) { // Optimization: don't bother measuring children who are going to use // leftover space. These views will get measured again down below if // there is any leftover space. if (isExactly) { mTotalLength += lp.leftMargin + lp.rightMargin; } else { final int totalLength = mTotalLength; mTotalLength = Math.max(totalLength, totalLength + lp.leftMargin + lp.rightMargin); } // Baseline alignment requires to measure widgets to obtain the // baseline offset (in particular for TextViews). The following // defeats the optimization mentioned above. Allow the child to // use as much space as it wants because we can shrink things // later (and re-measure). if (baselineAligned) { final int freeWidthSpec = MeasureSpec.makeSafeMeasureSpec( MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.UNSPECIFIED); final int freeHeightSpec = MeasureSpec.makeSafeMeasureSpec( MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED); child.measure(freeWidthSpec, freeHeightSpec); } else { skippedMeasure = true; } }
例子:
如果有这样的需求:
右边的箭头不想因为左边的字内容太多被挤掉可以这样写:
<LinearLayout android:id="@+id/select_deal_card_list_click" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="13dp" android:background="@drawable/selector_card_list_bg_end" android:clickable="true" android:orientation="horizontal" android:gravity="center" android:padding="7dp" > <TextView android:id="@+id/select_deal_card_list_content" android:layout_width="0dp" android:layout_height="wrap_content" android:text="2111111112222222/卡" android:singleLine="true" android:ellipsize="end" android:layout_weight="1" android:textColor="@color/common_gray_txt_bg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/select_ico" /> </LinearLayout>
0 0
- Android UI杂谈---layout_weight
- android UI设计 layout_weight解析
- android UI布局设计 —— layout_weight浅见
- android UI 之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:layout_weight
- Android:Layout_weight
- android;layout_weight
- Android:layout_weight
- android:layout_weight
- Android:layout_weight
- Android App整体架构设计的思考
- hdu2079 选课时间(题目已修改,注意读题)(多重背包)
- Sqlite中文乱码问题
- 存储过程的优点和缺点
- Spring 框架的 UI(IOC) 功能
- Android UI杂谈---layout_weight
- 完成命令解析程序
- 关于大型网站技术演进的思考(六)--存储的瓶颈(6)
- phpExcel常用方法
- <string.h>
- Java语言类库中几个常用的包
- poj1611 并查集
- 关于大型网站技术演进的思考(五)--存储的瓶颈(5)
- 为设置最顶层LinearLayout的width和height会导致:运行时,程序无反应,并显示“已停止”