Android - 布局方式學(xué)習(xí)

LinearLayout線性布局

1奉狈、線性布局方向有兩個(gè):horizontal央渣、 vertical 默認(rèn)horizontal
horizontal


image.png
horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/teal_200"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:text="LinearLayout布局1"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <Button
        android:text="LinearLayout布局2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

vertical


image.png
vertical
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/teal_200"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:text="LinearLayout布局1"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <Button
        android:text="LinearLayout布局2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

這里 layout_marginLeft相對(duì)于父視圖而言,layout_marginTop 相對(duì)于上面的 button 而言(如果上面沒(méi)有其他 view 那么就相對(duì)于父視圖射窒,如果有其他 view 那么就相對(duì)于離他最近的 view 來(lái)說(shuō))


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:textAllCaps="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />
</LinearLayout>

LinearLayout 嵌套使用


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/button_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:background="@color/red"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LinearLayout布局1"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="LinearLayout布局2"
            android:textAllCaps="false" />
    </LinearLayout>

</LinearLayout>

gravity藏杖、layout_gravity的使用


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:gravity="center_vertical"http://設(shè)置全部子視圖垂直方向居中
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:layout_gravity="center"http://設(shè)置居中顯示,但是線性布局限制了可布局的高度就是自己的高度上可水平移動(dòng)
        android:textAllCaps="false" />//設(shè)置 text 為字母時(shí) 默認(rèn)是大寫(xiě)的 這里設(shè)置為非大寫(xiě)

    <Button
        android:id="@+id/button_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:background="@color/red"
        android:layout_gravity="center"http://相對(duì)于外層來(lái)說(shuō)脉顿,該 view 在自己的高度范圍內(nèi)水平居中
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LinearLayout布局1"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="LinearLayout布局2"
            android:textAllCaps="false" />
    </LinearLayout>

</LinearLayout>

RelativeLayout相對(duì)布局

image.png

同 LinearLayout 一樣 也可以設(shè)置 gravity 來(lái)設(shè)置子視圖蝌麸。但是子視圖無(wú)法再設(shè)置 layout_gravity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_700">

    <Button
        android:id="@+id/relative_Btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="RelativeLayout布局 1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/relative_Btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/relative_Btn_1"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@+id/relative_Btn_1"http://這里通過(guò) id 獲取第一個(gè) btn就是相對(duì)于第一個(gè) button 設(shè)置布局
        android:text="RelativeLayout布局 2"
        android:textAllCaps="false" />

</RelativeLayout>

TableLayout表格布局

image.png

從圖中可以看出TableLayout布局默認(rèn)是垂直方向進(jìn)行布局的,而且寬度是填充屏幕的

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5個(gè)" />

</TableLayout>

和TableRow結(jié)合使用,發(fā)現(xiàn)超出屏幕部分會(huì)被截掉


image.png
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/blue">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第1個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第2個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第3個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第4個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第5個(gè)" />

    </TableRow>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5個(gè)" />

</TableLayout>
  • 和 TableRow 結(jié)合使用艾疟,及 collapseColumns 隱藏列("0,1,2")来吩,stretchColumns拉伸列(3)會(huì)把設(shè)置的拉伸列把剩余的空間拉伸滿(mǎn)。從圖中也可以看出這兩個(gè)屬性的設(shè)置只對(duì)一行多列的情況有作用蔽莱。這里還有一個(gè)情況就是設(shè)置以后如果有多行多列的情況弟疆,那么會(huì)把多行對(duì)應(yīng)的列都給隱藏或拉伸.
  • 子控件的兩個(gè)屬性:layout_column在第幾列開(kāi)始顯示;layout_span跨幾列顯示盗冷,就是寬度變成正常列寬的幾倍
    設(shè)置collapseColumns = "0,1"


    image.png

    設(shè)置 stretchColumns = "2,3"


    image.png
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:collapseColumns="0,1"
    android:stretchColumns="2,3"
    android:background="@color/blue">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第1個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第2個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第3個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第4個(gè)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第5個(gè)" />

    </TableRow>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4個(gè)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5個(gè)" />

</TableLayout>

GridLayout 網(wǎng)格布局

image.png

columnCount設(shè)置一行顯示幾列屬性(用戶(hù)水平布局)
rowCount 設(shè)置一列顯示幾行屬性(用于垂直布局)

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:background="@color/green">

    <Button
        android:id="@+id/grid_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1個(gè)" />

    <Button
        android:id="@+id/grid_btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2個(gè)" />

    <Button
        android:id="@+id/grid_btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="第3個(gè)" />

    <Button
        android:id="@+id/grid_btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4個(gè)" />

    <Button
        android:id="@+id/grid_btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5個(gè)" />

</GridLayout>

設(shè)置子視圖:layout_row 顯示在第幾行怠苔;layout_column顯示在第幾列;layout_columnSpan橫跨幾列顯示正塌。
以及columnWeight列權(quán)重顯示嘀略, rowWeight 行權(quán)重顯示


image.png
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/green">

    <Button
        android:id="@+id/grid_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1個(gè)" />

    <Button
        android:id="@+id/grid_btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="第2個(gè)" />

    <Button
        android:id="@+id/grid_btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:text="第3個(gè)" />

    <Button
        android:id="@+id/grid_btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="第4個(gè)" />
<!--這里有一個(gè)問(wèn)題 設(shè)置最后一個(gè)權(quán)重 rowWeight 沒(méi)有作用?-->
    <Button
        android:id="@+id/grid_btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5個(gè)" />

</GridLayout>

FrameLayout幀布局

FrameLayout布局兩個(gè)特有屬性 foreground 前景色 foregroundGravity前景色位置


image.png
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/red"
    android:foreground="@drawable/ic_baseline_person_24"
    android:foregroundGravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="FrameLayout學(xué)習(xí) 1"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="FrameLayout學(xué)習(xí) 2"
        android:textAllCaps="false"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

ConstraintLayout約束布局

1乓诽、使用ConstraintLayout設(shè)置 imageview 的時(shí)候需要注意帜羊,設(shè)置 srcCompat圖片資源的時(shí)候,預(yù)覽沒(méi)問(wèn)題鸠天,但是運(yùn)行的時(shí)候就會(huì)看不到圖片資源讼育。因?yàn)槭褂肅onstraintLayout,代碼中使用的是 imageview,但是設(shè)置圖片資源用的是 app:srcCompat奶段。我們知道 imageview 設(shè)置圖片資源的時(shí)候用的是 android:src 所以導(dǎo)致了加載不出來(lái)饥瓷。
2、從 Android23.3 開(kāi)始支持向量 drawable痹籍,只能通過(guò) app:srcCompat進(jìn)行加載了呢铆。
3、解決方案把 imageview 換成AppCompatImageView蹲缠;或者 activity 繼承AppCompatActivity


image.png

image.png
<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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.133"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.067" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="94dp"
        android:layout_height="102dp"
        android:layout_marginStart="124dp"
        android:adjustViewBounds="true"
        android:background="@color/purple_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.246"
        app:srcCompat="@drawable/pic1"
        tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末棺克,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子线定,更是在濱河造成了極大的恐慌娜谊,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斤讥,死亡現(xiàn)場(chǎng)離奇詭異纱皆,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)芭商,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)派草,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蓉坎,你說(shuō)我怎么就攤上這事澳眷。” “怎么了蛉艾?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵钳踊,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我勿侯,道長(zhǎng)拓瞪,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任助琐,我火速辦了婚禮祭埂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兵钮。我一直安慰自己蛆橡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布掘譬。 她就那樣靜靜地躺著泰演,像睡著了一般。 火紅的嫁衣襯著肌膚如雪葱轩。 梳的紋絲不亂的頭發(fā)上睦焕,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天藐握,我揣著相機(jī)與錄音,去河邊找鬼垃喊。 笑死猾普,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的本谜。 我是一名探鬼主播初家,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼耕突!你這毒婦竟也來(lái)了笤成?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤眷茁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后纵诞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體上祈,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年浙芙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了登刺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嗡呼,死狀恐怖纸俭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情南窗,我是刑警寧澤揍很,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站万伤,受9級(jí)特大地震影響窒悔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜敌买,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一简珠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧虹钮,春花似錦聋庵、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至宅倒,卻和暖如春攘宙,著一層夾襖步出監(jiān)牢的瞬間屯耸,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工蹭劈, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疗绣,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓铺韧,卻偏偏與公主長(zhǎng)得像多矮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子哈打,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

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