比如有如下一个布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.weight.MainActivity"    android:orientation="horizontal" >    <TextView        android:layout_width="0dp"        android:layout_height="48dp"        android:layout_weight="1"        android:background="#44ff0000"        android:gravity="center"        android:text="111111111111" />        <TextView        android:layout_width="0dp"        android:layout_height="48dp"        android:layout_weight="2"        android:background="#4400ff00"        android:gravity="center"        android:text="2" />        <TextView        android:layout_width="0dp"        android:layout_height="48dp"        android:layout_weight="3"        android:background="#440000ff"        android:gravity="center"        android:text="3" /></LinearLayout>


效果却是:




文字的底端是对齐的


想要得到预期的效果

需要加入:

android:baselineAligned="false"

效果如下图所示:




如果把第一个TextView的layout_weight属性设置为wrap_content

android:layout_width="wrap_content"


效果如下:


说明,LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配。



假设有如下布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.weight.MainActivity"    android:baselineAligned="false"    android:orientation="horizontal" >    <TextView        android:layout_width="match_parent"        android:layout_height="48dp"        android:layout_weight="1"        android:background="#44ff0000"        android:gravity="center"        android:text="111111111111" />        <TextView        android:layout_width="match_parent"        android:layout_height="48dp"        android:layout_weight="2"        android:background="#4400ff00"        android:gravity="center"        android:text="2" />        <TextView        android:layout_width="match_parent"        android:layout_height="48dp"        android:layout_weight="2"        android:background="#440000ff"        android:gravity="center"        android:text="3" /></LinearLayout>

假设外层的LinearLayout的width为480


参考:首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配


那么剩余的尺寸是:rest = 480 - 480 * 3 = - 480 * 2


TextView 1 : 480 + ( - 480 * 2 / 5) = 480 * (3 / 5)

TextView 2, 3 : 480 + ( - 480 * 2 ) * ( 2 / 5 ) = 480 * (1 / 5 )


效果如下图所示:




(剩余的尺寸可以是负值)

控件宽度 + 父控件剩余宽度 * 比例



实现只有一个TextView且只占用屏幕的一半:


<?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:weightSum="2"    android:orientation="horizontal" >        <TextView        android:layout_width="0dp"        android:layout_height="48dp"        android:layout_weight="1"        android:background="#44ff0000"        android:gravity="center"        android:text="111111111111" />    </LinearLayout>

使用 android:weightSum="2"


效果如下:






更多相关文章

  1. GitHub 上排名前 100 的 Android(安卓)开源库介绍
  2. android 美颜滤镜效果的实现
  3. Android:实现滑屏
  4. android加载gif图片的动画库
  5. Android中adapter的原理简单说明
  6. Android零碎知识点-更新中
  7. android背景图片及状态切换
  8. Android(安卓)UI开发第十篇――锁屏效果
  9. Android通过ImageView设置手指滑动控件缩放

随机推荐

  1. 3个月学会Android开发!Android面试资料集
  2. 抖音 Android 性能优化系列:新一代全能型
  3. 开发 Android 应用程序 常见问题 FAQ
  4. Android开发指南!带你全面解析Android框架
  5. 软件开发layer-list学习
  6. Android Handler的简单使用
  7. 抖音数据采集教程,Android群控黑盒调用,Sek
  8. React Native Android(安卓)开发巨坑
  9. android点滴(2)之Android安装环境变量的
  10. Android之Handler的post注意事项