Android_Layout_xml布局

1.构建xml布局文件

使用android提供的xml布局,可以快速构建UI界面,每个xml文件必须包含一个根元素,该xml文件位于res/layout/目录中。例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, I am a TextView" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, I am a Button" />
</LinearLayout>

2.加载xml布局文件

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	//加载xml布局文件应该位于首行
	setContentView(R.layout.activity_main);
}

3.属性ID

(1).xml中添加ID属性

android:id="@+id/button"

(2).xml中引用ID

android:id="@id/button"

(3).得到控件对象

Button myButton = (Button) findViewById(R.id.button);

4.布局的宽与高属性(layout_weight)

(1).layout_width 与 layout_height

所有视图都应该指定layout_width 与 layout_height属性,对该属性的描述主要有2个值

wrap_content根据控件内容的大小来定义当前控件整体大小

match_parent填满父类允许填满的空间位置,在 API Level 8之前使用fill_parent

一般在指定宽高时,推荐使用dp,而不是pixels。因为在不同的尺寸屏幕手机上,dp有更好的展示效果.

(2).关于android:layout_weight(需要有剩余空间)

在LinearLayout布局中,使用权重应该在布局中有剩余空间。在一个现象布局中有两个子现象布局设置宽带属性layout_width="wrap_content",此时屏幕还有剩余空间,如果指定了layout_width,系统将会把剩余空间平分,然后追加在各自的布局中。实现android:layout_weight需要在剩余空间的基础上,才能有更好的展示效果。如果2个假设定义了android:orientation="horizontal",且使用了android:layout_weight来表示子类控件的权重。layout_width有3中设置:

1).layout_width="wrap_content"

先将2个布局文件加载,将剩余空间平分,再追加,如果2个布局在layout_width="wrap_content"时宽度不一样,直接导致宽度不一样

2).layout_width="0dp"

这种方式能够达到平分的目的,应该使用layout_width="0dp",子类控件将根据权重大小等比宽度

3).layout_width="match_parent"

如果设置这种属性,由于没有了剩余空间,在系统计算的时候会产生一个负值,将根据权重等比缩小

5.layout_gravity与gravity

layout_gravity即设置当前视图相对于父类的位置摆放,gravity即控制当前视图的子类相对于父类的位置摆放。但有时layout_gravity不会起作用,原因很简单,就是忽略了父类的方位布局!

比如父类布局设置了android:orientation="vertical",而其子类有个button按钮,设置了android:layout_gravity="center_vertical",不会有任何效果,因为button遵循竖直排布,但只有水平方向的移动才会有效果!

因此,父类布局为vertical时,在设置layout_gravity时,只能控制水平方向的移动;在父类布局为horizontal时,在设置layout_gravity时,只能控制竖直方向的移动!而layout_gravity="center"即为可能有效果的那个方位居中!

6.margin与padding

主要用于控制布局的位置,利用盒子模型。

margin主要控制当前视图相对于父类视图相间的位置距离大小,即控制控件的外边框,如果为该控件设置背景,则不包含背景颜色

padding,主要用于控制内边距包含背景颜色,比如一个Button按钮,text="Bt";如果指定了android:paddingLeft="30dp",意为Bt距离当前控件的左边为30dp大小

7.常见布局

(1).LinearLayout

LinearLayout为一个视图组,所有子类按照一单方向排列,必须指定android:orientation属性,该值为horizontal或者vertical,当使用子类控件使用权重layout_weight时,宽或者高推荐使用0dp,

(2).RelativeLayout

RelativeLayout一个布局相对于另一个视图的位置,在上下左右或者相对于父类等等(16个)

属性一:(边缘对齐,左对右)相对于一个id控件

android:layout_below=""当前控件的上边缘对齐指定控件的下边缘(在同一直线上)
android:layout_above=""当前控件的下边缘对齐指定控件的上边缘
android:layout_toRightOf=""将当前控件的左边缘对其到指定控件的右边缘
android:layout_toLeftOf=""当前控件的右边缘对齐指定控件的左边缘

属性二:(边缘对其,左对左,只能针对兄弟控件)

android:layout_alignLeft=""左边缘对其做边缘
android:layout_alignRight=""右边缘对其做边缘

android:layout_alignTop=""上边缘对其做边缘

android:layout_alignBottom=""下边缘对其做边缘

android:layout_alignBaseline=""将当前控件的基本线与指定控件基本线对齐 文字基本线对齐
基本线对齐

属性三:(是否与父控件对齐)

android:layout_alignParentLeft="" 将当前控件的左边缘与父控件的左边缘对齐
android:layout_alignParentRight=""将当前控件的右边缘与父控件的右边缘对齐
android:layout_alignParentTop=""将当前控件的上边缘与父控件的上边缘对齐
android:layout_alignParentBottom=""将当前控件的下边缘与父控件的下边缘对齐
属性四:(居中于父控件)
android:layout_centerInParent="true"将当前控件置于父控件的水平垂直居中位置
android:layout_centerHorizontal="true"水平居中
android:layout_centerVertical="true"垂直居中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />

    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/name"
        android:layout_toLeftOf="@+id/times" />

    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/name" />

    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/times"
        android:text="@string/done" />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值