- 版權(quán)聲明:歡迎轉(zhuǎn)載疼约,轉(zhuǎn)載請注明出處卤档。 如果本文幫助到你,本人不勝榮幸程剥,如果浪費了你的時間劝枣,本人深感抱歉。如果有什么錯誤织鲸,請一定指出舔腾,以免誤導(dǎo)大家、也誤導(dǎo)我搂擦。感謝關(guān)注稳诚。
什么是布局?
布局是一種可以放置很多控件的容器瀑踢,他可以按照一定的規(guī)律調(diào)整內(nèi)部控件的位置扳还,從而編寫出好看的界面。布局的內(nèi)部也能夠放置布局橱夭,形成多層嵌套氨距。
線性布局 LinearLayout
LinearLayout 叫做線性布局,他會將其內(nèi)部的控件在線性的方向依次排列。 他是按照屬性android:orientation的指定棘劣,來決定其子視圖設(shè)置為水平還是垂直俏让。
- horizontal 表示一行 (水平) 默認的值
- vertical 表示一列 (垂直)
LinearLayout 是布局中的根視圖,因此應(yīng)將寬度和高度設(shè)置為 "match_parent"茬暇,從而填滿可供應(yīng)用使用的整個屏幕區(qū)域首昔。
栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
android:layout_gravity屬性:指定控件在布局中對齊方式,需要根據(jù)父視圖的排列方式來指定而钞,比如父視圖對齊方式為vertical沙廉,只有在水平方向上的對齊方式才有效
然后就是LinearLayout的重要屬性---android:layout_weight:一般稱為權(quán)重。他是使用比例的方式來指定控件的大小臼节。
栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Input someing"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
首先把width的值指定為0撬陵,可以理解成寬度不再受width控制,而由weight來控制网缝,系統(tǒng)會把所有的weight的值加起來巨税,然后按份分配給各個控件。另外粉臊,我們也可以只指定其中一部分控件的weight值來實現(xiàn)UI效果草添。
相對布局 RelativeLayout
他可以通過相對定位的方式讓控件出現(xiàn)在布局的任何位置《笾伲可以相對與父視圖定位远寸,也可以相對于控件定位
栗子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button2 " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3" />
</RelativeLayout>
android:layout_alignParentLeft:相對于父控件左對齊抄淑,其他的依次類推
android:layout_centerInParent:與父控件的中心對齊
栗子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 3"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/btn3"
android:layout_toLeftOf="@id/btn3"
android:text="Button 1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/btn3"
android:layout_toRightOf="@id/btn3"
android:text="Button 2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/btn1"
android:text="Button 4"/>
</RelativeLayout>
android:layout_above:讓一個控件位于其指定控件的上方,需要指定相對控件的id驰后,其他的依次類推
android:layout_toLeftOf:讓一個控件位于其指定控件的左側(cè)
android:layout_alignLeft:讓一個控件的左邊緣與另外一個控件的左邊緣對齊
這里我們需要注意的是:如果一個控件要引用另外一個控件的id時肆资,該控件一定要定義在引用控件的后面,不然會找不到id灶芝。
幀布局 FrameLayout
FrameLayout是最簡單的布局了郑原。應(yīng)用場景相對于前面兩種來說也少了很多。 所有放在布局里的控件夜涕,都按照層次堆疊在屏幕的左上角犯犁。后加進來的控件覆蓋前面的控件。
栗子:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#ff0000"
android:text="Text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
簡而言之女器,他根本控制不了子控件在其內(nèi)部的位置
百分比布局 Percent
可以不用match_parent或者wrap_content等方式來指定控件的大小酸役, 而是直接指定控件在布局中所占的百分比,可以輕松實現(xiàn)按任意比例分配布局的效果驾胆。
LinearLayout已經(jīng)可以使用權(quán)重來按比例分配控件大小了簇捍,所以只需要為FrameLayout和RelativeLayout提供擴展。
- PercentFrameLayout 繼承了FrameLayout的特性
- PercentRelativeLayout 繼承了RelativeLayout的特性
百分比布局屬于新增的布局俏拱,我們要想使用他暑塑,就需要在項目下的build.gradle 文件添加百分比布局庫的依賴。具體就是在dependencies閉包中添加以下內(nèi)容:
compile 'com.android.support:percent:24.2.1'
栗子:
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_gravity="left|top"
android:text="Button 1"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
<Button
android:layout_gravity="right|top"
android:text="Button 2"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
<Button
android:layout_gravity="left|bottom"
android:text="Button 3"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
<Button
android:layout_gravity="right|bottom"
android:text="Button 4"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
</android.support.percent.PercentFrameLayout>
由于PercentFrameLayout不是系統(tǒng)內(nèi)置的布局锅必,所以我們需要寫出完整的路徑事格,并且給他定義一個app的命名空間。所以我們可以使用app:layout_widthPercent與app:layout_heightPercent來指定控件的寬與高
好了搞隐,對于四種基本布局的基本用法暫時就了解到這里驹愚。慢慢用到新的知識再來分享。