android常見布局

線性布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".LayoutActivity">

<!--    match_parent 匹配父元素

        wrap_content包裹元素

        android:orientation="vertical"垂直布局

        orientation="horizontal"水平布局

        android:layout_weight="1"權(quán)重

        含義根據(jù)第一第二組體會(huì)一下吧韩肝,不固定等于1 還可以等于其他

        -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="第一組"/>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕1"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕2"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕3"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕4"/>

    </LinearLayout>

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="第二組"/>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕1"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="按鈕2"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="按鈕3"/>

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="按鈕4"/>

    </LinearLayout>

</LinearLayout>

效果

image

幀布局

所有的子控件都會(huì)在左上角砰碴,每個(gè)控件都會(huì)覆蓋前面的控件

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".LayoutActivity">

    <TextView

        android:layout_width="200dp"

        android:layout_height="200dp"

        android:text="我是1"

        android:gravity="right|bottom"

        android:textColor="#fff"

        android:background="#123"/>

    <TextView

        android:layout_width="150dp"

        android:layout_height="150dp"

        android:text="我是2"

        android:gravity="right|bottom"

        android:textColor="#fff"

        android:background="#234"/>

    <TextView

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:text="我是3"

        android:gravity="right|bottom"

        android:textColor="#fff"

        android:background="#456"/>

    <TextView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:text="我是4"

        android:gravity="right|bottom"

        android:textColor="#fff"

        android:background="#789"/>

</FrameLayout>

效果圖

image

相對(duì)布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".LayoutActivity">

    <TextView

        android:id="@+id/tv"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="相對(duì)布局"

        android:textSize="20dp"/>

    <EditText

        android:id="@+id/et"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/tv"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="登錄"

        android:layout_toLeftOf="@+id/btn2"

        android:layout_alignTop="@+id/btn2"

        android:layout_marginRight="20dp"/>

    <Button

        android:id="@+id/btn2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="注冊(cè)"

        android:layout_below="@+id/et"

        android:layout_alignParentRight="true"

        android:layout_marginRight="10dp"/>

</RelativeLayout>

效果:

image
可能會(huì)用到的 隨便卡卡
android:layout_toLeftOf 該組件在引用組件的左邊
android:layout_toRightOf 該組件在引用組件的右邊
android:layout_above 該組件在引用組件的上邊
android:layout_below 該組件在引用組件的下邊
android:layout_alignParentLeft 對(duì)齊父組件的左邊
android:layout_alignParentRight 對(duì)齊父組件的右邊
android:layout_alignParentTop 對(duì)齊父組件的上邊
android:layout_alignParentBottom 對(duì)齊父組件的下邊
android:layout_centerInParent 相對(duì)于父組件居中
android:layout_centerHorizontal 橫向居中
android:layout_centerVertical 垂直居中

表格布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:shrinkColumns="1"
    tools:context=".LayoutActivity">
<!--    shrinkColumns 當(dāng)頁面溢出的時(shí)候壓縮的元素,使表格能夠適應(yīng)父容器的大小
        stretchColumns 當(dāng)頁面不滿時(shí)拉伸的元素芳室,使能夠填滿表格中的空閑空間-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕2">
    </Button>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕3">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕4">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕5">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕6">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕7">
        </Button>
    </TableRow>


</TableLayout>

效果圖


圖片.png
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="2"
    tools:context=".LayoutActivity">
<!--    shrinkColumns 當(dāng)頁面溢出的時(shí)候壓縮的元素专肪,使表格能夠適應(yīng)父容器的大小
        stretchColumns 當(dāng)頁面不滿時(shí)拉伸的元素,使能夠填滿表格中的空閑空間-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1">
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕2">
    </Button>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕3">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕4">
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕5">
        </Button>
    </TableRow>


</TableLayout>

效果圖


圖片.png

壓縮和拉伸是按照0,1,2,3...排序的

網(wǎng)格布局

所有子控件默認(rèn)在GridLayout中橫向依次排列堪侯,當(dāng)只等每行的列數(shù)時(shí)嚎尤,到達(dá)指定列數(shù)

會(huì)自動(dòng)換行顯示

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"
    tools:context=".LayoutActivity">
    <TextView
        android:text="0"
        android:textColor="@android:color/black"
        android:textSize="40dp"
        android:layout_columnSpan="4"
        >

    </TextView>
    <Button
        android:text="清除"
        android:textColor="@android:color/black"
        android:textSize="26dp"
        android:layout_columnSpan="4"
        >
    </Button>
    <Button android:text="1" android:textSize="26dp"/>
    <Button android:text="2" android:textSize="26dp"/>
    <Button android:text="3" android:textSize="26dp"/>
    <Button android:text="+" android:textSize="26dp"/>
    <Button android:text="4" android:textSize="26dp"/>
    <Button android:text="5" android:textSize="26dp"/>
    <Button android:text="6" android:textSize="26dp"/>
    <Button android:text="-" android:textSize="26dp"/>
    <Button android:text="7" android:textSize="26dp"/>
    <Button android:text="8" android:textSize="26dp"/>
    <Button android:text="9" android:textSize="26dp"/>
    <Button android:text="*" android:textSize="26dp"/>
    <Button android:text="." android:textSize="26dp"/>
    <Button android:text="0" android:textSize="26dp"/>
    <Button android:text="=" android:textSize="26dp"/>
    <Button android:text="/" android:textSize="26dp"/>
</GridLayout>

效果圖:


圖片.png
可能會(huì)用到的 隨便卡卡
rowCount 幾行
columnCount 幾列
layout_rowSpan 合并行的個(gè)數(shù)
layout_columnSpan 合并列的個(gè)數(shù)
layout_column 在網(wǎng)格的第幾列
layout_row 在網(wǎng)格的第幾行
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市伍宦,隨后出現(xiàn)的幾起案子芽死,更是在濱河造成了極大的恐慌,老刑警劉巖次洼,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件关贵,死亡現(xiàn)場離奇詭異,居然都是意外死亡卖毁,警方通過查閱死者的電腦和手機(jī)揖曾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炭剪,你說我怎么就攤上這事练链。” “怎么了念祭?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵兑宇,是天一觀的道長。 經(jīng)常有香客問我粱坤,道長隶糕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任站玄,我火速辦了婚禮枚驻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘株旷。我一直安慰自己再登,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布晾剖。 她就那樣靜靜地躺著锉矢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪齿尽。 梳的紋絲不亂的頭發(fā)上沽损,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音循头,去河邊找鬼绵估。 笑死,一個(gè)胖子當(dāng)著我的面吹牛卡骂,可吹牛的內(nèi)容都是我干的国裳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼全跨,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼缝左!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起浓若,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤盒使,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后七嫌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡苞慢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年诵原,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绍赛,死狀恐怖蔓纠,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吗蚌,我是刑警寧澤腿倚,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站蚯妇,受9級(jí)特大地震影響敷燎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜箩言,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一硬贯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧陨收,春花似錦饭豹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至饵骨,卻和暖如春翘悉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宏悦。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國打工镐确, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人饼煞。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓源葫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親砖瞧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子息堂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354