Android各种Layout特性和使用汇总(一)

来源:互联网 发布:杭州司法拍卖淘宝网房产 编辑:程序博客网 时间:2024/05/16 06:56

LinearLayout 线性布局

特性:

横向或纵向,每行或每列(根据方向)只能有1个对象,可以设置对象在LinearLayout中占位的权重。
看上去很简单,也确实很简单,但LinearLayout却是Android中使用最多的布局之一,尤其是在多个Linear嵌套使用的时候,就会显得异乎寻常的强大。

例子:
1234567891011121314151617181920212223242526272829
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:text="Title" android:textSize="18dp" android:background="#AA3333FF"android:layout_width="fill_parent" android:layout_height="wrap_content"/><ListViewandroid:id="@+id/listViewInLinear"android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingTop="5dp" android:paddingBottom="5dp"android:layout_weight="1"/><LinearLayoutandroid:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="horizontal"><Button android:id="@+id/show"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Show" android:layout_weight="50"/><Button android:id="@+id/back"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Back" android:layout_weight="50"/></LinearLayout></LinearLayout>

下面是onCreate中ListView数据填充的方法,直接使用了APIDemo中的数据,用最简单的ArrayAdapter来实现。

1234567
ListView arrowList=(ListView) findViewById(R.id.listViewInLinear);arrowList.setAdapter(new ArrayAdapter<String>(this,            android.R.layout.simple_list_item_1, mStrings));//mString定义    private String[] mStrings = {            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", ...}

下面是运行截图,可以看到简单的2个LinearLayout嵌套外加layout_weight的使用,构造各分辨率自适应的布局是如此简单。
LinearLayout Sample截图
注意:同1个LinearLayout中可以设置多个子部件都有layout_weight属性,此时它们的大小会按weight的比例来进行分配,一般建议如果有多个weight,可以考虑让它们的和等于100。
另外,如果设置了layout_weight,那么与LinearLayout相同方向的大小设置不要设置fill_parent,而是设置wrap_content比较好,否则会发生问题。(具体情形遇见便知)

关键字:

orientation(方向),layout_weight(权重),嵌套

RelativeLayout 相对位置布局

特性:

布局内对象的位置可以相对于Layout本身来定位,也可以根据之前的对象来定位。非常适合用来表现多个对象对齐(或者故意不对齐?)的布局。
上面这段描述像不像什么都没说?其实RelativeLayout可以做的事情基本上都可以由多个LinearLayout来实现,但在条件适合的时候用RelativeLayout来实现会更方便,更强大。而且有些情况下几乎只有RelativeLayout才能实现了。
考虑以下这个布局的实现方式,
预想的布局
布局的第1行其实完全可以用LinearLayout来实现,只需要添加1个无文本的TextView在伸展区,并且设置layout_weight即可,虽然有些麻烦,但仍然可以实现。但第2行如何实现呢?要求是新的按钮与Right1右对齐。而用RelativeLayout就可以轻松的实现,下面是Layout定义文件:

例子:
12345678910111213141516171819202122
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><Button android:id="@+id/relaLeftButton" android:text="Left"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/><Button android:id="@+id/relaRight2Button" android:text="Right2"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignParentRight="true" android:layout_alignParentTop="true"/><Button android:id="@+id/relaRight1Button" android:text="Right1"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignTop="@id/relaRight2Button" android:layout_toLeftOf="@id/relaRight2Button"/><Button android:id="@+id/relaRight3Button" android:text="Right3Long"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_alignRight="@id/relaRight1Button" android:layout_below="@id/relaRight1Button"/></RelativeLayout>

在你使用了RelativeLayout之后,你会发现其中的控件都多了很多的layout_align开头的属性,有用于与父窗口对齐的,有用于和其它控件对齐和声明摆放位置的。如此便可以轻松的实现相对布局。下面是运行时截图:
Relative布局运行时截图
是不是达成要求了?虽然这是1个比较极端的例子,但在实际应用中,RelativeLayout的使用还是相当普遍的,通常用于登录画面之类的,元素不多,但要求布局相对宽松尽量利用空间的情况下。