显示元素布局
2014-02-16 22:15
183 查看
布局属性无效果时,可以通过设置weight来达到所要的效果。下面为android:layout_weight的详解。
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。
举个例子:比如说我们在水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)。
看例子:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="redwwwwwww"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
4000
android:layout_height="fill_parent"
android:layout_weight="3"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4"/>
</LinearLayout>
</LinearLayout>
android:layout_weight的详解来源:
http://www.cnblogs.com/alpha-bowen/archive/2011/03/02/1969343.html
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。
举个例子:比如说我们在水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)。
看例子:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="redwwwwwww"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
4000
android:layout_height="fill_parent"
android:layout_weight="3"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4"/>
</LinearLayout>
</LinearLayout>
android:layout_weight的详解来源:
http://www.cnblogs.com/alpha-bowen/archive/2011/03/02/1969343.html
相关文章推荐
- Ext FormPanel布局 (一行显示两对控件元素)
- css3 - 弹性盒布局 - 2.改变元素的显示顺序
- Ext FormPanel布局 (一行显示两对控件元素)
- 三种方式实现元素水平居中显示与固定布局和流式布局概念理解
- jQuery判断元素是否显示与隐藏
- Chrome下, iframe中PageUp时导致父/祖先元素显示错位
- Android 开发 软键盘覆盖而不上推布局 控件总显示在软件盘上边
- 关于 eclipse xml 文件的layout界面无法显示布局
- 【转载】HTML 元素的显示类型
- 读书笔记:微信小程序布局之行内元素和块级元素
- 微信中body的touchmove事件(下拉显示网址)影响元素的touchmove事件的解决方法
- 元素未显示设置width/height时IE中无法使用currentStyle获取(默认为auto)
- 七种布局显示方式效果及实现
- CSS定位元素居中显示
- 设置list-style-position,li元素嵌套块元素时,Firefox浏览器布局失效处理
- 通过索引查找二个容器中相同的元素,并分别显示相同的元素 以及删除相同元素之后的容器的内容
- 取消Webkit核心浏览器对输入框input元素高亮显示的方法
- Android Animation动画实战(一): 从布局动画引入ListView滑动时,每一Item项的显示动画
- 子线性布局中使两个控件在同一排显示的方法
- Framelayout布局中嵌套多个布局layout的显示