Android 第六课 4种基本布局之LinearLayout和Relativelayout
看完控件,緊接著看布局,布局是可以來(lái)放置控件,管理控件的。布局里也可以嵌套布局。
我們新建項(xiàng)目UILayoutTest項(xiàng)目,活動(dòng)名和布局名選擇默認(rèn)。加入活動(dòng)及其對(duì)應(yīng)的布局已經(jīng)創(chuàng)建完成。
- 線性布局(LinearLayout)
- android:layout_gravity屬性
- android:layout_weight屬性
- 相對(duì)布局(RelativeLayout)
- 相對(duì)于父布局定位
- 相對(duì)于控件定位:注意:當(dāng)一個(gè)控件去引用另一個(gè)控件的id時(shí),該控件一定要定義在引用控件的后面,不然會(huì)找不到id的情況
????????LinearLayout布局會(huì)將它所包含的控件在線性方向上一次排列,所謂線性方向,可能是垂直方向,或者是水平方向,前面我們都是在垂直方向上排列控件,我們可以通過(guò)android:orientation屬性指定了排列方向是vertical,如果指定的是horizontal,控件就會(huì)在水平方向上排列了。修改activity_layout.xml新增加三個(gè)按鈕,我們可以想不寫(xiě)android:orientation屬性,發(fā)現(xiàn)3個(gè)按鈕根據(jù)自身大小水平排列,因?yàn)檫@是LinearLayout布局默認(rèn)的控件排列方式,(倘若你的第一個(gè)控件的android:layout_width="match_parent",就是說(shuō)你的一個(gè)控件的寬度等于整個(gè)屏幕的寬度,那么你后面的哪個(gè)控件很有可能顯示不出來(lái)。)
等到我們寫(xiě)過(guò)android:orientation="vertical",我們會(huì)發(fā)現(xiàn)3個(gè)按鈕垂直的排列了。
????? ??
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>這里需要注意:如果LinearLayout的排列方式是horizontal,內(nèi)部的控件就不能將寬度指定為match_parent,因?yàn)檫@樣,單獨(dú)一個(gè)控件會(huì)將整個(gè)水平方向占滿,其他控件就沒(méi)有可以放置的位置了。同樣的道理如果LinearLayout的排列方式是horizontal,內(nèi)部控件就不能將高度指定為match_parent,因?yàn)檫@樣,單獨(dú)一個(gè)控件會(huì)將整個(gè)垂直方向占滿。
我們首先來(lái)看布局的android:layout_gravity屬性,我們之前看過(guò)android:gravity屬性,這兩者有些相似。區(qū)別是:android:gravity是指定文字在控件中的對(duì)齊方式,而android:layout_gravity是指定控件在布局中的對(duì)齊方式。兩者的可選值差不多,但是需要注意:LinearLayout的排列方向是horizontal時(shí),只有垂直方向上的對(duì)齊方式才會(huì)生效,因?yàn)榇藭r(shí)水平方向的長(zhǎng)度是不固定的,每增加一個(gè)控件,水平方向上的長(zhǎng)度都會(huì)改變,因而無(wú)法指定該方向上的對(duì)齊方式。同樣道理,當(dāng)LinearLayout的排列方向是vertical時(shí),只有水平方向上的對(duì)齊方式才會(huì)生效。修改activity_main.xml文件,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>我們?cè)賮?lái)看LineraLayout中另一個(gè)重要屬性android:layout_weight。它允許我們使用比例的方式來(lái)指定控件的大小,它可以去適應(yīng)手機(jī)的屏幕。下面我們來(lái)編輯一個(gè)消息發(fā)現(xiàn)界面,需要一個(gè)文本編輯框和一個(gè)發(fā)送按鈕,修改activity_main.xml中的代碼,如下所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="3"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="2"android:text="Send"/></LinearLayout>發(fā)現(xiàn)<EditText>和<Button>的寬度都指定為了0dp,不過(guò)你不用擔(dān)心文本編輯框和按鈕不存在,因?yàn)槲覀冇纸又褂昧薬ndroid:layout_weight屬性,此時(shí)控件大小由android:layout_weight來(lái)決定。這里0dp是一種比較規(guī)范的寫(xiě)法。另外,dp是Android中用于指定控件大小、間距等屬性的單位。在這里我們把<EditText>和<Button>的android:layou_weight分別設(shè)置為3和2,這表明EditText占整個(gè)屏幕寬度的3份,Button占2份(我們是將整個(gè)屏幕分為3+2份)。
當(dāng)然,我們也可以只是指定部分控件的layout_weight值來(lái)實(shí)現(xiàn)更好的效果。修改activity_main.xml文件中的代碼,如下:
我們只是指定了EditText的android:layout_weight屬性,并將Button的寬度改為wrap_content。這表明Button的寬度仍然按照wrap_content來(lái)計(jì)算,而EditText則會(huì)占滿屏幕的所有剩余空間。
- 相對(duì)布局(RelativeLayout)
- android:layout_alignParentLeft屬性可以讓控件和父布局的左上角對(duì)齊
- android:layout_above屬性可以讓一個(gè)控件位于另一個(gè)控件的上方
- android:layout_alignLeft表示讓一個(gè)控件的左邊緣和另一個(gè)控件的左邊緣對(duì)齊
相比較LinearLayout,RelativeLayout更加隨意一些,它通過(guò)相對(duì)定位的方式讓控件出現(xiàn)在布局的任何位置。正因?yàn)槿绱?#xff0c;RelativeLayout的屬性非常多,不過(guò)都是有規(guī)律可循的。修改activity_main.xml中的代碼,如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="button 5"/></RelativeLayout>我們讓Button 1和父布局的左上角對(duì)齊,Button 2和父布局的右上角對(duì)齊,Button 3居中顯示,Button 4 和父布局的左下角對(duì)齊,Button 5 和父布局的右下角對(duì)齊。程序運(yùn)行如下:
上面這是相對(duì)于父布局定位。
下面我們相對(duì)于控件進(jìn)行定位。
修改activity_main.xml中的代碼,如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_above="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 5"/></RelativeLayout>其中android:layout_above屬性可以讓一個(gè)控件位于另一個(gè)控件的上方,需要為這個(gè)屬性指定相對(duì)控件的id的引用。
android:layout_toLeftOf表示一個(gè)控件位于另一個(gè)控件的左側(cè)。注意:當(dāng)一個(gè)控件去引用另一個(gè)控件的id時(shí),該控件一定要定義在引用控件的后面,不然會(huì)找不到id的情況。重新運(yùn)行程序,如下圖:
RelativeLayout中還有另外一組相對(duì)于控件進(jìn)行定位的屬性,android:layout_alignLeft表示讓一個(gè)控件的左邊緣和另一個(gè)控件的左邊緣對(duì)齊,android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom道理是一樣的。
總結(jié)
以上是生活随笔為你收集整理的Android 第六课 4种基本布局之LinearLayout和Relativelayout的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java面试题39 给定includel
- 下一篇: Android 第七课 4种基本布局之F