Android按比例布局
来源:互联网 发布:海里知鱼是什么鱼 编辑:程序博客网 时间:2024/06/05 00:12
如果要实现在LinearLayout里实现按比例布局,一般采取的方式都是用weight来控制。
比如要实现几个控件分别按1:2:3的宽度来布局,父控件一般“layout_width”属性是“fill_parent”,关键是子控件的width和weight该怎么控制。
第一种情况,LinearLayout内的控件的layout_width设置为"wrap_content"
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="1"/>
</LinearLayout>
可以发现似乎可以实现按1、2、3的比例来控制子空间的宽度比。
但是当第一个textview里面的文本由1变成一长串字符串时,事情发生了变化。设备不会按照我们的比例进行分配宽度。
这个时候我们把子控件"layout_width"属性都设置为0dp,问题就得到了解决。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1111111111111111111111111111111111111111111"/>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="3"/>
</LinearLayout>
第二种情况,LinearLayout内的控件的layout_width设置为"fill_parent"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
</LinearLayout>
这个时候可以发现两个子空间所占宽度与所设置的"weight"数值成反比。再看看三个色块的效果。占宽度与所设置的发现第三块色块只显示了一部分或者干脆没显示,说明如果用"fill_parent"还是会有一些问题的,就是显示不全的问题。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#aa0000"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00aa00"
android:gravity="center"
android:text="2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#0000aa"
android:gravity="center"
android:text="3"/>
</LinearLayout>
所以,按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。
- Android按比例布局
- Android按比例布局
- Android按比例布局
- Android 线性布局按比例显示
- Android中元素按比例布局
- 按比例布局
- (转)android 按比例布局 适应不同分辨率
- Android 控件在布局中按比例放置 (
- Android 控件在布局中按比例放置
- android layout 按比例布局的代码
- android layout 按比例布局的代码
- Android中按比例显示的布局
- android XML布局和子View按比例布局
- android XML布局和子View按比例布局
- Android按比例布局之layout_weight和weightSum的使用
- QGridLayout按比例设定布局
- xib等按比例布局
- LinearLayout使用weightSum按比例布局
- 黑马程序员---Objective-C学习笔记之一
- spring的ContextLoaderListener
- Hadoop小试之WordCount
- HDU 1241 Oil Deposits
- list initializing a vector demo
- Android按比例布局
- C编译器剖析_4.3 语义检查_语句statement
- java笔记(网络编程)
- 稍大的串
- 如何将.FRM,.MYD,MYI文件转换为.SQL导入数据库中
- Intent
- MyEclipse10中安装freeMarker插件
- 二分·二分查找之k小数
- 数据库基础知识