UI布局初試three---四種基本布局

  • 版權(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來指定控件的寬與高

好了搞隐,對于四種基本布局的基本用法暫時就了解到這里驹愚。慢慢用到新的知識再來分享。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末劣纲,一起剝皮案震驚了整個濱河市逢捺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌癞季,老刑警劉巖劫瞳,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異绷柒,居然都是意外死亡志于,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門废睦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來伺绽,“玉大人,你說我怎么就攤上這事∧斡Γ” “怎么了澜掩?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長杖挣。 經(jīng)常有香客問我输硝,道長,這世上最難降的妖魔是什么程梦? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮橘荠,結(jié)果婚禮上屿附,老公的妹妹穿的比我還像新娘。我一直安慰自己哥童,他們只是感情好挺份,可當(dāng)我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著贮懈,像睡著了一般匀泊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上朵你,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天各聘,我揣著相機與錄音,去河邊找鬼抡医。 笑死躲因,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的忌傻。 我是一名探鬼主播大脉,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼水孩!你這毒婦竟也來了镰矿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤俘种,失蹤者是張志新(化名)和其女友劉穎秤标,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宙刘,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡抛杨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荐类。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片怖现。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屈嗤,到底是詐尸還是另有隱情潘拨,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布饶号,位于F島的核電站铁追,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏茫船。R本人自食惡果不足惜琅束,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望算谈。 院中可真熱鬧涩禀,春花似錦、人聲如沸然眼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽高每。三九已至屿岂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鲸匿,已是汗流浹背爷怀。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留带欢,地道東北人霉撵。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像洪囤,于是被迫代替她去往敵國和親徒坡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,969評論 2 355

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,152評論 25 707
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程瘤缩,因...
    小菜c閱讀 6,424評論 0 17
  • 布局是一種可用于放置很多控件的容器喇完,它可以按照一定的規(guī)律調(diào)整內(nèi)部控件的位置,從而編寫出精美的界面剥啤。當(dāng)然锦溪,布局的內(nèi)部...
    TommySH閱讀 1,219評論 0 1
  • Android布局是應(yīng)用界面開發(fā)的重要一環(huán),在Android中府怯,共有五種布局方式刻诊,分別是: LinearLayou...
    楓羽望空閱讀 9,753評論 1 9
  • 我對你所有的愛和熱情好像都在那個陽光明媚的午后,夾著海風(fēng)混著淚水牺丙,流進海里则涯,再也回不來了复局。 一下午的眼淚把你從我...
    我在夢見你s閱讀 205評論 0 0