[Android開發(fā)]--六大基本布局

1鸽疾、線性布局(LinearLayout):按照垂直或者水平方向布局的組件
2吊洼、幀布局(FrameLayout):組件從屏幕左上方布局組件
3、表格布局(TableLayout):按照行列方式布局組件
4制肮、絕對布局(AbsoluteLayout):按照絕對坐標(biāo)來布局組件
5冒窍、相對布局(RelativeLayout):相對其它組件的布局方式
6、約束布局 (ConstraintLayout):按照約束布局組件

線性布局LinearLayout

線性布局效果圖.png
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_dark"
            android:padding="15dp"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="Text1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray"
            android:padding="15dp"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="Text2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_dark"
            android:padding="15dp"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="Text3" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_light"
            android:padding="15dp"
            android:textAlignment="center"
            android:text="Text4" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            android:padding="15dp"
            android:textAlignment="center"
            android:text="Text5" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_green_dark"
            android:padding="15dp"
            android:textAlignment="center"
            android:text="Text6" />
    </LinearLayout>
</LinearLayout>

線性布局特點豺鼻,同一級組件之間沒有互相依賴综液,按照添加順序依次排列,其中線性布局最重要的屬性是android:orientation儒飒,通過該屬性可以指定水平方向排列還是縱向排列

幀布局FrameLayout

幀布局效果圖.png
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="@android:color/holo_green_dark" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="@android:color/holo_blue_bright" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@android:color/holo_orange_light" />


</FrameLayout>

幀布局會按照添加順序?qū)盈B在一起谬莹,默認(rèn)層疊在左上角位置,本例子因為設(shè)置了layout_gravity="center"桩了,所以層疊在視圖中心位置

表格布局TableLayout

表格布局效果圖.png
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_green_dark"
    android:shrinkColumns="0,2"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我占據(jù)一行" />

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="111111111111111111111111" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="222222222222222222222222" >
        </Button>
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="000000000000000000000000" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="222222222222222222222222" >
        </Button>
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="000000000000000000000000" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="我占據(jù)2列" >
            <!--android:layout_span表示合并單元格個數(shù)-->
        </Button>
    </TableRow>
</TableLayout>

TableLayout繼承LinearLayout,有元素單獨占一行可以不寫TableRow
TableLayout的一些屬性:

android:shrinkColumns 設(shè)置可收縮的列
android:stretchColumns 設(shè)置可伸展的列
android:collapseColumns 設(shè)置要隱藏的列
默認(rèn)所有的列為stretchColumns届良,如只有一個控件則獨占一行
每列寬度的計算原則:首先根據(jù)可伸展列根據(jù)其內(nèi)容最長的一行占據(jù)一行的百分比,再根據(jù)可收縮的列等分剩余的部分
例如圖中圣猎,優(yōu)先計算“ 111111111111111111111111”的寬度士葫,再由剩下的列等分剩余的空間
如果遇到空間已經(jīng)不足了,則剩余的列均不顯示
控件的一些屬性:
android:layout_column表示當(dāng)前控件在第幾列
android:layout_span表示合并單元格個數(shù)

絕對布局AbsoluteLayout

難以實現(xiàn)多分辨率適配送悔,不建議使用

相對布局RelativeLayout

相對布局效果圖.png
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_blue_bright"
        android:padding="20dp"
        android:text="text1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/text1"
        android:layout_alignLeft="@+id/text1"
        android:background="@android:color/holo_green_dark"
        android:padding="20dp"
        android:text="text2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_alignLeft="@+id/text1"
        android:background="@android:color/holo_orange_light"
        android:padding="20dp"
        android:text="text3"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/text1"
        android:layout_alignTop="@+id/text1"
        android:background="@android:color/holo_red_dark"
        android:padding="20dp"
        android:text="text4"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text1"
        android:layout_alignTop="@+id/text1"
        android:background="@android:color/holo_purple"
        android:padding="20dp"
        android:text="text5"/>

</RelativeLayout>

相對布局常用屬性:

子類控件相對子類控件:值是另外一個控件的id

android:layout_above----------位于給定DI控件之上
android:layout_below ----------位于給定DI控件之下

android:layout_toLeftOf -------位于給定控件左邊
android:layout_toRightOf ------位于給定控件右邊

android:layout_alignLeft -------左邊與給定ID控件的左邊對齊
android:layout_alignRight ------右邊與給定ID控件的右邊對齊
android:layout_alignTop -------上邊與給定ID控件的上邊對齊
android:layout_alignBottom ----底邊與給定ID控件的底邊對齊

android:layout_alignBaseline----對齊到控件基準(zhǔn)線

android:layout_above="@id/xxx" --將控件置于給定ID控件之上
android:layout_below="@id/xxx" --將控件置于給定ID控件之下

android:layout_toLeftOf="@id/xxx" --將控件的右邊緣和給定ID控件的左邊緣對齊
android:layout_toRightOf="@id/xxx" --將控件的左邊緣和給定ID控件的右邊緣對齊

android:layout_alignLeft="@id/xxx" --將控件的左邊緣和給定ID控件的左邊緣對齊
android:layout_alignTop="@id/xxx" --將控件的上邊緣和給定ID控件的上邊緣對齊
android:layout_alignRight="@id/xxx" --將控件的右邊緣和給定ID控件的右邊緣對齊
android:layout_alignBottom="@id/xxx" --將控件的底邊緣和給定ID控件的底邊緣對齊
android:layout_alignParentLeft="true" --將控件的左邊緣和父控件的左邊緣對齊
android:layout_alignParentTop="true" --將控件的上邊緣和父控件的上邊緣對齊
android:layout_alignParentRight="true" --將控件的右邊緣和父控件的右邊緣對齊
android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對齊
android:layout_centerInParent="true" --將控件置于父控件的中心位置
android:layout_centerHorizontal="true" --將控件置于水平方向的中心位置
android:layout_centerVertical="true" --將控件置于垂直方向的中心位置

相對父容器慢显,值是true或false
android:layout_alignParentLeft ------相對于父靠左
android:layout_alignParentTop-------相對于父靠上
android:layout_alignParentRight------相對于父靠右
android:layout_alignParentBottom ---相對于父靠下

android:layout_centerInParent="true" -------相對于父即垂直又水平居中
android:layout_centerHorizontal="true" -----相對于父即水平居中
android:layout_centerVertical="true" --------相對于父即處置居中

相對于父容器位置:
android:layout_margin="10dp"
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom

版本4.2以上相對布局新屬性
android:layout_alignStart---------------------將控件對齊給定ID控件的頭部
android:layout_alignEnd----------------------將控件對齊給定ID控件的尾部
android:layout_alignParentStart--------------將控件對齊到父控件的頭部
android:layout_alignParentEnd---------------將控件對齊到父控件的尾部

android:layout_marginLeft指該控件距離邊父控件的邊距,
android:paddingLeft指該控件內(nèi)部內(nèi)容欠啤,如文本距離該控件的邊距荚藻。

約束布局ConstraintLayout

約束布局效果圖.png
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:padding="5dp"
        android:text="手機號"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:padding="5dp"
        android:text="驗證碼"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:hint="請輸入手機號"
        android:inputType="textPersonName"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="請輸入驗證碼"
        android:inputType="textPersonName"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView2"
        app:layout_constraintStart_toEndOf="@+id/textView2"
 />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="10dp"
        android:text="獲取驗證碼"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/editText2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="開始"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市洁段,隨后出現(xiàn)的幾起案子应狱,更是在濱河造成了極大的恐慌,老刑警劉巖祠丝,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疾呻,死亡現(xiàn)場離奇詭異除嘹,居然都是意外死亡,警方通過查閱死者的電腦和手機岸蜗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門尉咕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人璃岳,你說我怎么就攤上這事年缎。” “怎么了铃慷?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵单芜,是天一觀的道長。 經(jīng)常有香客問我犁柜,道長缓溅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任赁温,我火速辦了婚禮,結(jié)果婚禮上淤齐,老公的妹妹穿的比我還像新娘股囊。我一直安慰自己,他們只是感情好更啄,可當(dāng)我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布稚疹。 她就那樣靜靜地躺著,像睡著了一般祭务。 火紅的嫁衣襯著肌膚如雪内狗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天义锥,我揣著相機與錄音柳沙,去河邊找鬼。 笑死拌倍,一個胖子當(dāng)著我的面吹牛赂鲤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播柱恤,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼数初,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了梗顺?” 一聲冷哼從身側(cè)響起泡孩,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎寺谤,沒想到半個月后仑鸥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吮播,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年锈候,在試婚紗的時候發(fā)現(xiàn)自己被綠了薄料。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡泵琳,死狀恐怖摄职,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情获列,我是刑警寧澤谷市,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站击孩,受9級特大地震影響迫悠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜巩梢,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一创泄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧括蝠,春花似錦鞠抑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至法绵,卻和暖如春箕速,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背朋譬。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工盐茎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人徙赢。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓庭呜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親犀忱。 傳聞我的和親對象是個殘疾皇子募谎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容