Android常用布局的xml属性

来源:互联网 发布:网站赌博可以改数据吗 编辑:程序博客网 时间:2024/06/07 02:02

Android中常用布局—LinearLayout和RelativeLayout 的xml属性

LinearLayout和RelativeLayout共有属性:

控件ID
android:id=”@+id/btn”

控件宽度:
android:layout_width=”80px”  //”80dip”或”80dp”
android:layout_width =“wrap_content”
android:layout_width =“match_parent”

控件高度:
android:layout_height=”80px”  //”80dip”或”80dp”
android:layout_height =“wrap_content”
android:layout_height =“match_parent”

控件与控件的 间距:
android:layout_marginLeft=”5dip”    //控件距离 左边控件 5dip以上
android:layout_marginRight=”5dip”   //距离右边
android:layout_marginTop=”5dip”   //距离上面
android:layout_marginRight=”5dip”   //距离下面

控件显示位置:
android:gravity=”center”  //left,right, top, bottom
android:gravity=”center_horizontal”

android:layout_gravity=”center_vertical”
android:layout_gravity=”left”
android:layout_gravity=”left|bottom”

android:layout_gravity  则设置控件本身相对于父控件的显示位置
android:gravity     布局内部的子元素的位置。

控件的文本描述
android:text=”@String/text1”  //在string.xml中定义text1的值
android:textSize=”20sp”
android:textColor=”#ff123456”
android:textStyle=”bold”    //普通(normal), 斜体(italic),粗斜体(bold_italic)

定义控件是否可见:
android:visibility=”visible”     //可见
android:visibility=”invisible”    //不可见,但是在布局中占用的位置还在
android:visibility=”gone”     //不可见,完全从布局中消失

定义背景图片:
android:background=”@drawable/img_bg”   //img_bg为drawable下的一张图片

seekbar控件背景图片及最大值
android:progressDrawable=”@drawable/seekbar_img”
android:thumb=”@drawable/thumb”
android:max = “60”

仅在RelativeLayout中有效:

在父亲布局的相对位置
android:layout_alignParentLeft=”true”   //控件 对齐 父布局左边
android:layout_alignParentRight=”true”   //控件 对齐 父布局右边
android:layout_alignParentTop=”true”   //控件 对齐 父布局上面
android:layout_alignParentBottom=”true ”  //控件 对齐 父布局下面

android:layout_centerHorizontal=”true”   //水平居中
android:layout_centerVertical=”true”
android:layout_centerInParent=”true”

在某个控件的相对位置
android:layout_toRightOf=”@id/button1”   //在控件button1的右边,不仅仅是紧靠着
android:layout_toLeftOf=”@id/button1”   //在控件button2的左边,不仅仅是紧靠着
android:layout_below=”@id/button1 ”    //在控件button1下面,不仅仅是正下方
android:layout_above=“@id/button1”    //在控件button1下面,不仅仅是正下方

定义和某控件对齐
android:layout_alignTop=”@id/button1”   //和控件button1上对齐
android:layout_alignBottom=”@id/button1”   //和控件button1下对齐
android:layout_alignLeft=”@id/button1”   //和控件button1左对齐
android:layout_alignRight=”@id/button1”   //和控件button2右对齐

仅在LinearLayout中有效

设置控件在一排或一列中所占比例值
android:layout_weight=”1”

控件排布:
android:orientation=”horizontal”  //水平排布
android:orientation=”vertical“   //竖直排布