在android中的全部组件都有android:layout_height,android:layout_width,android:layout_weight这三个属性,所以今天写了这篇总结,总结关于组件布局的问题。
各个属性可以设置的值:
android:layout_height:wrap_content,match_parent(fill_parent),px
(pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)
android:layout_width:wrap_content,match_parent(fill_parent),px
(pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)
android:layout_weight:整数
针对3种情况展开讨论:
假想一个列表框,需要放置3个组件,每个组件的长度可以是需要变化的(如TextView随文本的长度变化),或者是长度固定的(如ImageView组件)。
每一个格子里放一种组件,所以有8种情况,针对这8种情况分别对这3个组件进行这3个属性的配置:
(在同一个LinearLayout如果不设比重则LinearLayout会先满足第一个组件的长和宽,不管后面几个组件是否还有显示的空间)
变变变:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="阿斯顿发萨达发萨达发萨达发射的阿斯顿发阿斯顿发" />
</LinearLayout>
变变不:(image宽度不在比重分配的尺寸之中)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1的阿斯顿发阿sadfasdfasdfsadfsadfsd斯顿发" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="2顿发阿斯asdfa sdfadsfsdfasdfasdfasdfadsf顿发" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
</LinearLayout>
变不变:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1的阿斯顿发阿sadfasdfasdfsadfsadfsd斯顿发" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="2顿发阿斯asdfa sdfadsfsdfasdfasdfasdfadsf顿发" />
</LinearLayout>
变不不:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
</LinearLayout>
不不不:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
</LinearLayout>
不不变:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
</LinearLayout>
不变不:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
</LinearLayout>
不变变:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jiantou1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
</LinearLayout>
细心的读者可能已经明白其中的规律!
在layout_width设置为fill_parent的时候,layout_weight所代表的是你的控件要优先尽可能的大,但这个大是有限度的,即fill_parent.
在layout_width设置为wrap_content的时候,layout_weight所代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
layout_height 同 layout_width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2顿发阿斯asdfa ssdfsdgsdfgdfadsfsdfasdfasdfasdfadsf顿发" />
</LinearLayout>
水平线性按比例分配android:layout_weight="1"时也可将组件的android:layout_width="0dp",效果同android:layout_width="wrap_content",按正比例分配除去固定分配掉的尺寸所剩余的尺寸。
当View的width,height为wrap_content或者0dp/px/sp时,若设定了weight,那么layout就会从剩余的未确定的layout中,根据weight的总和,也就是weightSum来计算需要为这样的View设置多少长宽。
width=leftWidth*weight/weightSum;
height=leftHeight*weight/weightSum;
另外weight的计算和布局,在linearLayout中Onlayout和OnMeasure函数。
分享到:
相关推荐
本篇文章介绍了android弹出activity设置大小的方法,需要的朋友可以参考一下
封装的工具设计思路,首先做长宽压缩,如果图片太大,先做长宽压缩,暂定大于2M(大小读者可以自己设置)循环做长宽压缩(暂定长宽都压缩一半,山样读者可以自己设置大小),其次做质量压缩,大于1M,循环做质量压缩...
Android中DatePicker与TimePicker的使用
调整图片大小长宽的组件.zipIOS应用例子源码下载调整图片大小长宽的组件.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
ios应用源码之调整图片大小长宽的组件 .
IOS应用源码之调整图片大小长宽的组件 .rar
IOS应用源码——调整图片大小长宽的组件.zip
调整图片大小长宽的组件_IOS应用源码.rar
soft_用ASP判断图像长宽的组件.zip.zip
c# 屏幕部分截图 可以选取截图起始点 和长宽 存在c盘
soft_用ASP判断图的长宽的组件.zip.zip
Android应用开发详解(原始BMP文件编号)Android应用开发详解(原始BMP文件编号)Android应用开发详解(原始BMP文件编号)
液晶屏幕尺寸计算excel,方便查阅,自动计算长宽比
从网络异步加载图片资源,主线程执行ui操作,工作线程执行耗时操作。并可对图片触摸放大缩小。
Android本身并没有直接提供表格这个控件,一般可以用GridView来实现比较简单,但是每个数据显示的长宽会一样。当然,也可以用ListView来实现,这个灵活定义每个数据显示的长宽。还有一种,是采用布局TableLayout来...
WPF改变已生成的控件的长宽 比如说,我生成了一个界面,界面上有个按钮,长宽定了 我拖动gridSplitter 计算坐标,可以修改按钮的长宽 内部使用Tabcontrol做例子的
叶片长宽的测量常采用旋转法,其在测量时需多次旋转叶片直至外接矩形面积最小为止,耗时较大;由于叶片形状各异,旋转法的测量准确性也不高。鉴于此,给出了运用矩测量叶片长宽的方法,用零阶矩和一阶矩求得叶片重心...
如:系统里的config.sys就是系统自动配置文件,它知是在用户开机这段时间对计算机进行初始化设置,也就是用户对系统的设置都由它来对计算机进行恢复。 4、system:Android 系统文件。主要包括usr(用户文件夹,包含...
将各种显示器的英寸转为日常使用的厘米,自己开发。