1.android:layout_weight
android:layout_weight这个属性用于设置组件的重要程度,重要程度越高,其值的越小,所有的view的layout_weight缺省值都是为0
要实现下面布局:
将xml文件写成这样就可以了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView1"
android:background="#ff0000"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="填满其他区域"
android:layout_weight="1"
>
</EditText>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>
当Button1 和 TextView的android:layout_width都设置为match_parent或者是fill_parent
且Button1的android:layout_weight的值2,TextView的android:layout_weight的值1时,可以使TextView占据整个宽度的2/3
注意其前提条件是:他们的父布局的android:layout_width要设置为match_parent或者是fill_parent,否则Button1和TextView都会被压缩成最小宽度。
当将TextView的android:layout_width的值设置为wrap_content时即使TextView的android:layout_weight的值1,Button1的android:layout_weight的值2,也不会有上面的效果,原因是wrap_content将组建的宽度压缩了如图所示:
对于EditText如果不将android:layout_weigh属性设置为一个大于0的数,Button2将会看不到,因为EditText的android:layout_width设置为match_parent了
2.android:padding和android:layout_margin
android:padding是设置View中的内容距离View边缘的距离,android:layout_maegin是设置View中内容距离其他View或父容器边缘的距离,当父容器中只有一个View时,这俩个属性的作用是一样的。
单位问题:px(像素) in(英寸) mm(毫米) pt(一个物理点,1/72英寸) dp(与密度无关的像素) sp(与比例无关的像素)
其中 px in mm pt 用来设置绝对尺寸的单位,不会随着屏幕分辨率的变化做出相应的调整,而dp sp 则会自动适应屏幕分辨率的变化,所以除了特殊处理,一般用dp作为尺寸单位,sp设置字体大小的单位。
dp 和 sp都是相对于160dpi尺寸的单位,一般320*160或类似大小的屏幕的分辨率是160dpi,在这种情况下,sp dp px 所显示的效果都是一样的。当换到240dpi的分辨率下,sp 和dp 都会乘以一个系数:240/160,而px不变。
注意:android:layout_gravirt并不是所有的情况都起作用,比如:当View在水平布局中的时候,android:layout_gravirt属性只有垂直方向上的值起作用。同理,在垂直布局中的时候,只有水平方向上的值起作用。
在水平布局下:
在垂直布局下:
效果如下:
其中 px in mm pt 用来设置绝对尺寸的单位,不会随着屏幕分辨率的变化做出相应的调整,而dp sp 则会自动适应屏幕分辨率的变化,所以除了特殊处理,一般用dp作为尺寸单位,sp设置字体大小的单位。
dp 和 sp都是相对于160dpi尺寸的单位,一般320*160或类似大小的屏幕的分辨率是160dpi,在这种情况下,sp dp px 所显示的效果都是一样的。当换到240dpi的分辨率下,sp 和dp 都会乘以一个系数:240/160,而px不变。
3.android:gravity和android:layout_gravity
android:layout_gravty指定View在父容器中的位置,而android:gravity则是指定View内部内容(文本,图像,View)的位置注意:android:layout_gravirt并不是所有的情况都起作用,比如:当View在水平布局中的时候,android:layout_gravirt属性只有垂直方向上的值起作用。同理,在垂直布局中的时候,只有水平方向上的值起作用。
在水平布局下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_gravity="center"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_horizontal"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_vertical"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
结果:
在垂直布局下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff0000"
android:orientation ="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ff00"
android:orientation ="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_vertical"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000ff"
android:orientation ="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center_horizontal"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</LinearLayout>
效果如下:
更新中....