Android開發(fā)-UI布局

Android中所有的UI(用戶界面)都是使用View和ViewGroup對象建立的,View是一個可以將一些信息繪制在屏幕上并與用戶產(chǎn)生交互的對象ViewGroup是一個可以包含多個的View和ViewGroup的容器,用來定義UI布局。

Android提供了一系列View和ViewGroup子類,開發(fā)者可以靈活的組合使用它們來完成界面布局、界面元素繪制和用戶交互等工作

開發(fā)者還可以選擇性地繼承一些系統(tǒng)提供的View,來定義View,把自己定義的界面元素顯示給用戶。

Android的UI開發(fā)使用層次模型來完成,一般都是在一個ViewGroup中嵌套多層ViewGroup,每層中含有任意數(shù)目的View,但最好不要超過十層

常用的布局

  • LinearLayout——線性布局

  • RelativeLayout——相對布局

  • FrameLayout——幀布局

  • TableLayout——表格布局

  • AbsoluteLayout——絕對布局

  • GridLayout——網(wǎng)格布局

布局定義方式

定義UI布局的最常用的方法是使用XML布局文件,如同HTML一樣,XML為布局提供了一種可讀的結(jié)構(gòu)。XML中的每個元素都是,View或ViewGroup的子孫的對象組成的樹。樹根是一個ViewGroup對象,所有的葉節(jié)點都是View對象,樹的分支節(jié)點都是ViewGroup對象。

Android中是ViewGroup可以嵌套包含很多View以及ViewGroup對象,ViewGroup是View的子類。

Android UI屏幕適配

  • 屏幕尺寸:指屏幕的對角線長度,單位為英寸,1英寸== 2.45cm

  • 屏幕分辨率:指水平和垂直方向的像素點個數(shù),單位px,1px== 1像素點,一般以垂直像素*水平像素,例如 1920 * 1080

  • 屏幕像素密度:指每英寸上的像素點數(shù),單位是dpi,dpi:dot per inch。屏幕像素密度與屏幕尺寸有關(guān)。

  • px:像素點,構(gòu)成圖像的最小單位

  • dip:device independent pixels(設(shè)備獨立像素),也是密度無關(guān)像素。以160dpi為基準,1dip=1px。

  • dp:與dip相同。

  • sp:專門用于字體的像素單位,一般定義為偶數(shù)。

LinearLayout (線性布局)

1. vertical / horizontal

使用android:orientation= "vertical/horizontal"定義排列方向

//linearlayout.xml

<?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" >
    
    <!--線性布局是按照水平和垂直排布的,默認是水平排布的
        orientation:用來指定當前線性布局排布的方向
        horizontal:水平
        vertical:垂直-->

</LinearLayout>

2. margin / padding

屬性 含義
android:layout_marginXXX(XXX:left/tpo) 設(shè)置該控件距離左、上邊界的距離
android:paddingXXX(XXX:left/top) 設(shè)置控件內(nèi)部距離控件邊緣的距離
  • 區(qū)分:
    margin:表示控件距離其他或者屏幕邊緣的間距。---外邊距
    padding:表示控件的內(nèi)部內(nèi)容距離控件邊緣的間距。---內(nèi)邊距
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="30dp"
        android:text="Button" />
        <!--wrap_content :代表包裹內(nèi)容,適合內(nèi)容大小
            match_parent :父類大小
            layout_margn是指組件距離父窗體的距離,
            padding是指組件中的內(nèi)容距離組件邊緣的距離-->

3. gravity / layout_gravity

屬性 含義
android:gravity 用于設(shè)置該View內(nèi)部內(nèi)容的對齊方式
android:layout_gravity 用于設(shè)置該View在其父類中的對齊方式
  • 注意:如果線性布局的排布方式為水平,那么layout_gravity在水平方向上就不起作用,只有在垂直方向上起作用,反之亦然。

  • 可選值包括:
    left、right、top、bottom、center、horizon
    center_vertical、center_horizontal
    bottom\center_horizontal(使用\組合使用)

4. layout_weight (LinearLayout特有屬性)

LinearLayout特有屬性,android:layout_weight,表示比重,可實現(xiàn)百分比布局,如果控件為 match_parent,則layout_weight的值與占比重反相關(guān),即:其值越大,占用比例越小,如果控件為 wrap_content,則對比重判斷則為正相關(guā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="vertical" 
    android:weightSum="3">
    <!--總的權(quán)重-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:layout_weight="2">
        <!--綠色區(qū),權(quán)重為2-->
    </LinearLayout>
    
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:layout_weight="1">
        <!--紅色區(qū),權(quán)重為1-->
    </LinearLayout>
</LinearLayout>
  • 注意:
    若在一個頁面中,有一個有權(quán)重控件,和一個無權(quán)重的控件,系統(tǒng)會先給無權(quán)重的控件分配空間后,才會得到需要百分比的空間大小,進行百分比劃分。權(quán)重布局只存在于LinearLayout中,其余布局無效。

RelativeLayout(相對布局)

RelativeLayout(相對布局):按照控件相互之間的相對位置來確定,RelativeLayout中往往需要定義每一個控件的資源ID。

1. 常用屬性

常用屬性 含義
layout_alineParentXXX(XXX:方向) 對齊方式
android:layout_marginXXX 外部間距
android:paddingXXX(XXX:left/top) 內(nèi)部間距
android:layout_centerVertical 垂直居中
android:layout_centerHorizontal 水平居中
android:layout_centerInParent 父類的中部

注意:layout_widthlayout_height是在平面圖形中不可或缺的兩個屬性,任何圖形需要顯示都需要寬和高。

2. android:layout_toRightOf(在某個控件的右方)

<Button
    android:id="@+id/button1"
    android:layout_weight="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="30dp"
    android:padding="20dp"
    android:text="Button"/>
    <!--@+id:表示系統(tǒng)本來不存在的對應(yīng)ID值,其方向性的單詞可以更換。
        以上Button可作為參照物-->
<Button
    android:layout_weight="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id+button1"
    android:layout_alingnBottom="@+id/button1"/>    
    <!--在控件的右方、底部-->

3. anignBaseline (基準線對齊)

4. layout_below (在XX的下方)

  • 注意:

    針對相對布局而言,一般不會過多給定很多想關(guān)聯(lián)的屬性,否則耦合性就會大大增加。相對布局的重點在于理解控件id,在相對布局中的控件一般都存在id屬性,+id表示是系統(tǒng)中本來不存在的對應(yīng)的id值,需要將這個id值添加到系統(tǒng)中,@id表示從系統(tǒng)中去除已經(jīng)添加好的id。

TableLayout (表格布局)

TableLayout屬于行和列形式的管理控件,每行為一個TableRow對象也可以是一個View對象。在TableRow中還可以繼續(xù)添加其他控件,每添加一個子控件就成一列,TableLayout不會生成邊框。

TableLayout是繼承至LinearLayout,即TableLayout有LinearLayout的屬性(見上文)。同時也有自己的特有屬性:

XML屬性名 說明
android:collapseColumns 設(shè)置指定的列為collapse,該列會被隱藏
android:shrinkColumns 設(shè)置指定的列為shrinkable,該列的寬度進行收縮,自適應(yīng)父類容器的大小
android:stretchColumns 設(shè)置指定的列為stretch,該列會被拉伸,填充滿表格的空白區(qū)域
<?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="match_parent" 
    android:stretchColumns="0,1,2"
    android:shrinkColumns="0" 
    android:collapseColumns="2">
    <!--stretch拉伸1,2,3列,
        shrinkable收縮第一列,
        collapse隱藏第一列
        拉伸后收縮時因為,如果第一列內(nèi)容過多,會覆蓋第二三列,
        因此要收縮第一列,以顯示所有內(nèi)容-->
    
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!--此處Table可不設(shè)定寬高值,系統(tǒng)自動給定-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp"
            android:text="姓名"/>
        <!--TableLayout是LinearLayout的子類,可設(shè)置gravity、padding等屬性-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="電話"/>
     </TableRow>
     
     <TableRow
        android:layout_width="match_patent"
        android:layout_height="wrap_content"
        android:"right">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="總計:100"/>
     </TableRow>
     <!--此TableRow未遵循此表格布局-->
</TableLayout>

FrameLayout (幀布局)/ AbsoluteLayout (絕對布局)

FrameLayout(幀布局)默認是按左上角(0,0)開始排布,在幀布局下的每一個控件都是以畫面的形式進行呈現(xiàn),最開始定義的控件出現(xiàn)在最下方,最后定義的控件出現(xiàn)在最上方,一般用于手機聯(lián)系人的導(dǎo)航顯示字母、幀動畫等內(nèi)容。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:text="你好"/>
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="30dp"
        android:background="#ff0000"
        android:text="頁面"/>

</FrameLayout>

AbsoluteLayout(絕對布局),又叫做坐標布局,可以直接指定子元素的絕對位置,這種布局簡單、直觀性強。由于手機屏幕尺寸差別較大,使用絕對定位適應(yīng)性會比較差,不推薦使用。一般用于固定屏幕手機。

屬性 含義
android:layout_x 確定X坐標,以左上角為頂點
android:layout_y 確定Y坐標,以左上角為頂點
  • 注意:若不設(shè)置layout_x和layout_y,那么他們的默認值是(0,0)他們會出現(xiàn)在左上角。

GridLayout (網(wǎng)格布局)

GridLayout(網(wǎng)格布局)是在android4.0版本以上出現(xiàn)的布局,可以實現(xiàn)并行和并列的效果。

常用屬性 含義
android:layout_columnSpan 擴展列的數(shù)目
android:layout_rowSpan 擴展行的數(shù)目
android:layout_gravity 填充方式
columnCount 定義存在多少列
rowCount 定義存在多少行
  • 注意:

    GridLayout與TableLayout有什么不同?

    TableLayout定義TableRow來呈現(xiàn)內(nèi)容,GridLayout可以直接定義控件來使用,并且TableLayout不可以合并行或列。

計算器頁面的實現(xiàn)

計算器頁面.jpg
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:columnCount="4">
<!--使用columnCount定義一共4列-->
    <!--放置按鍵-->
    <Button 
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"/>

    <Button 
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"/>
        
    <Button 
        android:id="@+id/num3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"/>
        
     <Button 
        android:id="@+id/chu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"/>
        
    <Button 
        android:id="@+id/num4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"/>

    <Button 
        android:id="@+id/num5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"/>
        
    <Button 
        android:id="@+id/num6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"/>
        
     <Button 
        android:id="@+id/cheng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"/>

    <Button 
        android:id="@+id/num7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"/>

    <Button 
        android:id="@+id/num8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"/>
        
    <Button 
        android:id="@+id/num9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"/>
        
     <Button 
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"/>

     <Button 
        android:id="@+id/num0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="0"/>
    <!--columnSpan合并兩列g(shù)ravity填充整個區(qū)域-->
    <Button 
        android:id="@+id/dian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."/>
        
    <Button 
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:text="+"/>
        
     <Button 
        android:id="@+id/deng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="="/>
</GridLayout>

Android UI開發(fā)分類

  1. 界面布局開發(fā)——定義界面的布局
  2. 控件開發(fā)——定義單個界面元素
  3. AdapterView與Adapter開發(fā)——列表顯示(適配器的開發(fā))
  4. UI組件開發(fā)——對話框、通知、菜單等
  5. 自定義View、圖形圖像和動畫——通過代碼,自行繪制界面。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容