android 布局权重问题(最近布局经常坑爹)
来源:互联网 发布:淘宝怎么买vr资源 编辑:程序博客网 时间:2024/05/01 00:52
With layout_weight you can specify a size ratio between multiple views. E.g. you have a MapView and a table which should show some additional information to the map. The map should use 3/4 of the screen and table should use 1/4 of the screen. Then you will set the layout_weight of the map to 3 and the layout_weight of the table to 1.
To get it work you also have to set the height or width (depending on your orientation) to 0px.
//权重和父容器orientation有关
horizontal 指水平方向权重 android:layout_width
vertical 指垂直方向权重 android:layout_height
Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Button1" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:
我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:
得出结论:在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.
2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Button1" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="2"
android:text="Button2" />
</LinearLayout>
同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:
这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:
我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了
- android 布局权重问题(最近布局经常坑爹)
- android 布局权重问题(最近布局经常坑爹)
- Android 布局中权重的问题
- android 布局文件的权重问题
- Android布局中权重设置问题
- Android布局和权重
- android学习遇到的问题之LinearLayout布局权重问题
- android studio layout 布局权重
- 剖析Android 线性布局中的权重(layout_weight)
- 剖析Android 线性布局中的权重(layout_weight)(解答疑惑)
- 小玩Android布局中的weight(权重)
- 剖析Android 线性布局中的权重(layout_weight)(解答疑惑)
- 布局中使用权重对齐问题
- Android线性布局权重Weight效果展示
- 剖析Android线性布局中 Layout_weight(权重)
- 线性布局 权重 详解
- LinearLayout权重布局
- android 布局问题(1)
- 表某一位为特定值的记录排在最前
- 给视图添加映射 并使之弹跳
- 嵌入式根文件系统的移植和制作详解
- MFC界面 坐标获取/转换 及 区域获取
- Oracle 的 procedure
- android 布局权重问题(最近布局经常坑爹)
- UI移动和渐隐渐现
- 解决PLSQL developer 提示乱码问题
- 简单的带动画渐隐效果
- c++ 运算符优先级 来自 c++ primer
- MSRA面经
- PhoneGap与Android之间通信原理
- Java框架介绍:Quartz从入门到进阶
- WPF实现DoEvents