layout_weight及常见属性解析
2016-04-23 12:44
435 查看
比如有如下一个布局文件:
效果却是:
文字的底端是对齐的
想要得到预期的效果
需要加入:
效果如下图所示:
如果把第一个TextView的layout_weight属性设置为wrap_content
效果如下:
说明,LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配。
假设有如下布局文件:
假设外层的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且只占用屏幕的一半:
使用 android:weightSum="2"
效果如下:
<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"
效果如下:
相关文章推荐
- 第九周上机实践项目-阅读项目
- The Java™ Tutorials
- 必要时进行保护性拷贝
- 拿到阿里,网易游戏,腾讯,smartx的offer的过程 (转)
- HTTP状态码
- QT Layout 布局的重要性
- jsp页面报 $ is not defined
- spring多数据源配置
- JVM内存GC机制
- 下载老版本的xcode
- 数据结构--单链表
- 面试之1604
- 【浅谈bootsrap响应式布局】
- Linux Shell处理文本最常用的工具大盘点
- ubuntu安装jdk-6u45-linux-x64-rpm.bin
- 【Android 学习】小知识Notification的新旧用法
- 从“架构师书单”讲开去
- ajax
- Windows服务定时执行任务
- 简单的cookie读写封装