Android UI杂谈---layout_weight
2016-03-30 21:15
344 查看
对Android 中 LinearLayout中属性layout_weight讨论:
(一)
(1)当android:orientation=”horizontal”android:layout_width=”0dp”时:
表现:
(2)当
android:orientation=”horizontal”android:layout_width=”match_parent”时:
表现:
(3)当
android:orientation=”horizontal”且 android:layout_width=”wrap_content”时:
表现:
总结:
表现最佳的是第一种,第二种把第三个TextView挤掉了,第三种没有按比例排列,完全失去了layout_weight属性的作用。
从源码角度分析:
google LinearLayout源码:
例子:
如果有这样的需求:
右边的箭头不想因为左边的字内容太多被挤掉可以这样写:
(一)
(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>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories