LinearLayout的layout_weight的使用

1.LinearLayout内的控件的layout_width设置为”wrap_content”

<LinearLayout  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="#ff0000" android:gravity="center" android:text="1"/>    <TextView  android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="#00ff00" android:gravity="center" android:text="2"/>    <TextView  android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="#0000ff" android:gravity="center" android:text="3"/></LinearLayout>

效果如下:

可以看到这三个TextView是按照1:2:3的比例进行显示的。
问题:
TextView的文本较长,TextView的宽度就会增加。

<LinearLayout  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="#ff0000" android:gravity="center" android:text="111111111111111111"/>    <TextView  android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" android:background="#00ff00" android:gravity="center" android:text="2"/>    <TextView  android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:background="#0000ff" android:gravity="center" android:text="3"/></LinearLayout>

效果如下:

解决方法
将控件的android:layout_width由”wrap_content”修改为”0dp”:

<LinearLayout  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="#ff0000" android:gravity="center" android:text="111111111111111111"/>    <TextView  android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#00ff00" android:gravity="center" android:text="2"/>    <TextView  android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:background="#0000ff" android:gravity="center" android:text="3"/></LinearLayout>

效果如下:

2.LinearLayout内的控件的layout_width设置为”match_parent”

<LinearLayout  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="#ff0000" android:gravity="center" android:text="1"/>    <TextView  android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:background="#00ff00" android:gravity="center" android:text="2"/>    <TextView  android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:background="#0000ff" android:gravity="center" android:text="3"/></LinearLayout>

效果如下:

第三个TextView丢掉了。
把weight分别改为2,2,3,效果如下:

效果让人很困惑。

正确方法:
按比例显示LinearLayout内各个子控件,水平方向的设置android:layout_width=”0dp”,竖直方向的设置android:layout_height=”0dp”。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

更多相关文章

  1. android RecyclerView基本使用(上)
  2. Android之指南针学习
  3. android开发积累2-实现自定义android控件
  4. Adapter 优化图片复用,导致界面图片出现闪烁效果
  5. ANDROID L——RecyclerView,CardView导入和使用
  6. 【Android(安卓)界面效果43】Android(安卓)LayoutInflater的infl
  7. TranslateAnimatin 实现的动态菜单
  8. android shape的使用 自定义 控件形状
  9. RecyclerView加载不同view实现效果--IT蓝豹

随机推荐

  1. [Android Pro] Android 打包流程
  2. TextView 加链接所有方法
  3. Android:用定时器 timer 刷新界面
  4. Android textview 显示不全的问题
  5. 离线快速安装 Android SDK and AVD Manag
  6. Android 5.0 Phone初始化分析
  7. Android开发-进阶:详解Android中AsyncTas
  8. Android 第三方应用监听home键
  9. Android 日期控件属性
  10. android:stateNotNeeded="true"