Android 布局筆記

像海浪撞過了山丘以后還能撐多久
他可能只為你贊美一句后往回流
那嬌艷的花盛開后等你來能撐多久
還是被詩人折斷了傷心了
換歌詞一首
那鴛鴦走散了一只在拼命的往南走
被混沌的城市用鋼筋捂住了出口
仿佛悲傷的人們
能靠著霧霾遮住傷口
還羨慕著期待藍(lán)天的少年總抬頭
.....

前言

通過 L1-1B 的課程學(xué)習(xí)潘拱,我 get 到了 ViewGroup 和兩種基本布局 LinearLayout(線性布局)哈雏、RelativeLayout(相對(duì)布局) 的知識(shí),那么現(xiàn)在我們來一一了解這些東西吧廷臼。

1. ViewGroup
  • 什么是 ViewGroup ? 運(yùn)用官放文檔的原話

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams
class which serves as the base class for layouts parameters.

簡單的翻譯一些就是:ViewGroup 是一個(gè)可以包含其他視圖(稱為子對(duì)象)的特殊視圖。視圖組是布局和視圖容器的基類。 這個(gè)類還定義了 ViewGroup.LayoutParams 類,用作布局參數(shù)的基類骤铃。(渣渣英語勿噴)
根據(jù)字面上的解釋應(yīng)該很好了解了,如果還想深入了解請(qǐng)移步到 官網(wǎng)溃列。

2. LinearLayout(線性布局)

A Layout that arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation(). You can also specify gravity, which specifies the alignment of all the child elements by calling setGravity() or specify that specific children grow to fill up any remaining space in the layout by setting the weight member of LinearLayout.LayoutParams. The default orientation is horizontal.

渣渣英語又來翻譯了:將其子元素排列在單列或單行中的布局劲厌。 可以通過調(diào)用 setOrientation() 設(shè)置行的方向。 您還可以通過調(diào)用 setGravity() 來指定所有子元素的對(duì)齊方式听隐,或指定特定子項(xiàng)通過設(shè)置 LinearLayout.LayoutParamsweight 成員來填充布局中的任何剩余空間补鼻。 默認(rèn)方向?yàn)?水平

  • 現(xiàn)在我們來看 LinearLayout 的簡單用法
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小一"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小二"
        android:textColor="#1d953f"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小三"
        android:textColor="#f15b6c"
        android:textSize="24sp"/>
</LinearLayout>

我在 **LinearLayout **中添加了 3 個(gè) TextView ,每個(gè) TextView 的長和寬都是 wrap_content ,并指定排序方式為 vertical 雅任,然后它就會(huì)如下圖顯示一樣:

Linear1.png

現(xiàn)在我們來看 LinearLayout 的屬性

** android:orientation="vertical" ** :LinearLayout 的排序方式风范,有 vertical(垂直方向)horizontal(水平方向)

現(xiàn)在我們將 android:orientation 的屬性改為 horizontal

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    ...

</LinearLayout>

修改代碼后 LinearLayout 中的控件會(huì)在水平方向上依次排列,如下圖

linear2.png

根據(jù)官方原話說:可以通過調(diào)用 setGravity() 來指定所有子元素的對(duì)齊方式 沪么,那么現(xiàn)在我們?cè)趤砜?android:layout_gravity 屬性
二話不說硼婿,先上代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小一"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp"
        android:layout_gravity="top"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小二"
        android:textColor="#1d953f"
        android:textSize="24sp"
        android:layout_gravity="center_vertical"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是小三"
        android:textColor="#f15b6c"
        android:textSize="24sp"
        android:layout_gravity="bottom"/>
    
</LinearLayout>

我將第一個(gè) TextView 的對(duì)其方式指定為 top,第二個(gè) TextView 的對(duì)其方式指定為 center_vertical禽车,第三個(gè) TextView 的對(duì)其方式指定為 bottom寇漫,因?yàn)楫?dāng)前 LinearLayout 的排列方式為 horizontal,所以當(dāng)前的效果如下圖顯示:

linear3.png

在學(xué)習(xí) LinearLayout 過程中我還學(xué)到了一個(gè)重要的屬性
android:layout_weight (權(quán)重) 殉摔,現(xiàn)在我們來看它的用法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:text="我是小一"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#1d953f"
        android:text="我是小二"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#f15b6c"
        android:text="我是小三"
        android:textSize="24sp"/>

</LinearLayout>

根據(jù)代碼顯示州胳,當(dāng)前 LinearLayout 的排列方式為 horizontal,我把 3 個(gè) TextViewandroid:layout_width 屬性設(shè)為 ''0dp''逸月,然后在每個(gè) TextView 中加入了 android:layout_weight="1" 屬性栓撞,然后它的效果就會(huì)如下圖顯示:

linear4.png

根據(jù)上圖分析可以得到玛迄,我加入了 android:layout_weight="1" 屬性之后欧引,3 個(gè) TextVeiw 就平分了整個(gè)屏幕,每個(gè) TextVeiw 各占一份蝎毡,從而得到 權(quán)重 是與 屏幕比例相關(guān)的

注意: 當(dāng)我們?cè)谒椒较蛏鲜褂?weight 屬性的時(shí)候笛钝,控件所占屏幕比和控件的高度本身沒有任何關(guān)系壶熏,只是和高度的值有關(guān)系容客。反之一樣

  • 到現(xiàn)在為止關(guān)于 LinearLayou 的屬性就介紹到這里了艾恼,如果想深入了解,請(qǐng)查閱官方文檔剃浇。
3. RelativeLayout(相對(duì)布局)

關(guān)于 RelativeLayout 我就不想過多的做解釋了巾兆,在這里主要放出官方文檔的原意和基本屬性。

A Layout where the positions of the children can be described in relation to each other or to the parent. 點(diǎn)擊跳轉(zhuǎn)到官方
上面的意思就是:可以相對(duì)于彼此或相對(duì)于父母描述子元素的位置的布局虎囚。

  • 基本屬性:
    • android:layout_alignParentTop="true":將部件的頂部與容器的頂部對(duì)齊
  • android:layout_alignParentBottom="true":將部件的底部與容器的底部對(duì)齊
  • android:layout_alignParentStart="true":將部件的左側(cè)與容器的左側(cè)對(duì)齊
  • android:layout_alignParentEnd="true":將部件的右側(cè)與容器的右側(cè)對(duì)齊
  • android:layout_centerInParent="true"":部件在容器中水平垂直居中
  • android:layout_centerHorizontal="true"":部件在容器中垂直居中
  • android:layout_centerVertical="true"":部件在容器中水平居中
  • android:layout_below="@+id/控件名字":該控件在某控件的下方
  • android:layout_above="@+id/控件名字":該控件在某控件的上方
  • android:layout_toStartOf="@+id/控件名字":該控件在某控件的左方
  • android:layout_toEndOf="@+id/控件名字":該控件在某控件的右方
4. 總結(jié)

好了,就到這里了蔫磨,其實(shí)關(guān)于布局不止有 LinearLayout(線性布局)淘讥、RelativeLayout(相對(duì)布局),在 Android 中還有 FrameLayout(幀布局)堤如、AbsoluteLayout(絕對(duì)布局)蒲列,TableLayout(表格布局)、GridLayout(網(wǎng)格布局)搀罢、PercentLayout(百分比布局)蝗岖、ConstraintLayout(約束布局)等等。以上就是我通過 L1-1B 的課程學(xué)習(xí)到的知識(shí)點(diǎn)榔至,雖然很簡單抵赢,但做任何是都是由簡單到復(fù)雜的!

                 混子性打野唧取,等我混起來之時(shí)铅鲤,就是我 carry 之日
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市枫弟,隨后出現(xiàn)的幾起案子邢享,更是在濱河造成了極大的恐慌,老刑警劉巖淡诗,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件骇塘,死亡現(xiàn)場離奇詭異,居然都是意外死亡韩容,警方通過查閱死者的電腦和手機(jī)款违,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宙攻,“玉大人奠货,你說我怎么就攤上這事∽颍” “怎么了递惋?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵柔滔,是天一觀的道長。 經(jīng)常有香客問我萍虽,道長睛廊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任杉编,我火速辦了婚禮超全,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘邓馒。我一直安慰自己嘶朱,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布光酣。 她就那樣靜靜地躺著疏遏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪救军。 梳的紋絲不亂的頭發(fā)上财异,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音唱遭,去河邊找鬼戳寸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛拷泽,可吹牛的內(nèi)容都是我干的疫鹊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼跌穗,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼订晌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蚌吸,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤锈拨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后羹唠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奕枢,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年佩微,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缝彬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡哺眯,死狀恐怖谷浅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤一疯,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布撼玄,位于F島的核電站,受9級(jí)特大地震影響墩邀,放射性物質(zhì)發(fā)生泄漏掌猛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一眉睹、第九天 我趴在偏房一處隱蔽的房頂上張望荔茬。 院中可真熱鬧,春花似錦竹海、人聲如沸慕蔚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽坊萝。三九已至,卻和暖如春许起,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背菩鲜。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國打工园细, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人接校。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓猛频,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蛛勉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鹿寻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,106評(píng)論 25 707
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,429評(píng)論 2 45
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程诽凌,因...
    小菜c閱讀 6,408評(píng)論 0 17
  • 早晨下雨的時(shí)候毡熏,吹好的頭發(fā)不小心淋了點(diǎn)雨,變得慘不忍睹侣诵,在公司尬了一天痢法。 下班回家路上,不時(shí)用手抓頭發(fā)杜顺,然后...
    CalmEsae閱讀 182評(píng)論 1 0
  • 三草兩木卸妝霜 2.卸妝泡沫:清潔能力比較強(qiáng)财搁,帶有硅膠刷頭,適合油性或混合性偏油 干性以及...
    LL莉子閱讀 169評(píng)論 0 0