日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

        歡迎訪問 生活随笔!

        生活随笔

        當前位置: 首頁 > 运维知识 > Android >内容正文

        Android

        Android - 布局详解之LinearLayout与RelativeLayout

        發布時間:2024/7/19 Android 54 豆豆
        生活随笔 收集整理的這篇文章主要介紹了 Android - 布局详解之LinearLayout与RelativeLayout 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

        本篇博文只針對LinearLayout與RelativeLayout

        我們在新建一個布局文件時,一般遵循這樣的思路:先確定該文件對應的界面中各個布局和控件的位置和大小,然后再來設置各個布局和控件的其他屬性,如背景、文字等。


        ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??上篇 ? ?確定控件的位置和尺寸
        在確定各個布局和控件的位置和大小時,首先需要考慮的是最外層的Layout的位置,有如下兩種方法可以采用:
        1、直接設置最外層的Layout填充父窗體,即: android:layout_width="match_parent" android:layout_height="match_parent" ?, ?這將不涉及Layout與父窗體邊緣的距離設置。
        2、設置最外層Layout的長寬中的一個或兩個的值剛好適應子布局和控件的尺寸,即: 設置android:layout_width=" ?"、android:layout_height=" ?"中的一個或兩個屬性的值為wrap_content, 這種情況下,Layout默認是左邊緣和上邊緣與父窗體對齊,如果想改變它的顯示位置,可使用如下幾種方式:
        A、直接用一個具體的值來指定Layout與父窗體邊緣的距離,需要使用這幾個屬性:
        android:layout_marginLeft設置該Layout距離父窗體左邊緣的距
        android:layout_marginTop設置該Layout距離父窗體上邊緣的距離
        ??? B、用非具體數值的方式來確定Layout在父窗體中的位置,需要使用這幾個屬性: android:layout_gravity=" ?", 可以選擇的常用值有:center_vertical、center_horizontal(豎直居中、水平居中)等 但需要注意的是,使用A、B兩種方式是會有沖突的,這會增加維護的難度,最好不要同時使用。
        上述解決了Layout在父窗體中的位置和大小設置的問題,LinearLayout與RelativeLayout皆適用。 那么,一個?LinearLayout或RelativeLayout中的控件(布局)的位置?又是怎么來確定呢?
        在界面比較復雜的情況下,我們可以先將這個Layout中的所有子Layout和控件都視為子控件,待處理完這個Layout中的子控件的位置后,再來處理子Layout中的控件的位置和大小,有點類似遞歸的思想。
        同樣,在設置Layout中的控件的位置時,LinearLayout與RelativeLayout也有很多可以共用的屬性 比如使用如下幾種方式來設置: A、直接用一個具體的值來指定Layout中的內容與Layout邊緣的距離,需要使用這幾個屬性:
        android:paddingLeft設置該Layout中的內容距離該Layout左邊緣的距離
        android:paddingTop設置該Layout中的內容距離該Layout上邊緣的距離
        android:paddingRight設置該Layout中的內容距離該Layout右邊緣的距離
        android:paddingBottom設置該Layout中的內容距離該Layout下邊緣的距離

        B、用非具體數值的方式來確定該Layout中的內容的位置,需要使用以下屬性: android:gravity ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 可以選擇的常用值有:center_vertical、center_horizontal、right(豎直居中、水平居中、靠右)等 但需要注意的是,使用A、B兩種方式是會有沖突的,這會增加維護的難度,最好不要同時使用。
        除此之外,在設置Layout中的控件的位置時,LinearLayout與RelativeLayout也有很多不同的屬性。 接下來分析,LinearLayout與RelativeLayout的屬性有何不同?在使用時怎么選擇?
        一、LinearLayout
        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 {@link #setOrientation(int) setOrientation()}. * You can also specify gravity, which specifies the alignment of all the child elements by* calling {@link #setGravity(int) setGravity()} or specify that specific children * grow to fill up any remaining space in the layout by setting the <em>weight</em> member of* {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}.* The default orientation is horizontal.*/ //LinearLayout 將它的子布局(控件)排成一行或一列,可以調用setOrientation()方法來設置排列 //的方向,或者調用setGravity()方法 //來指定子布局(控件)的對齊方式,還可以設置weight的值 //來改變子布局(控件)的填充范圍,默認排列方向是horizontal public class LinearLayout extends ViewGroup {} 在設置Layout里邊的控件的位置時,LinearLayout中的控件可以使用的屬性有:
        A、設置該控件距左、上、右、下邊(無論是父控件還是兄弟控件)的長度(在RelativeLayout中也適用):
        android:layout_marginLeft設置該控件距左邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginTop設置該控件距上邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginRight設置該控件距右邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginBottom設置該控件距下邊(無論是父控件還是兄弟控件)的長度
        ? ?? B、用非具體數值的方式來確定該控件在父窗體中的位置,需要使用這幾個屬性: android:layout_gravity=" ?", 可以選擇的常用值有:center_vertical、center_horizontal(豎直居中、水平居中)等 C、在控件內用android:layout_weight 屬性修改控件在父控件中的填充比例 關于這個屬性的使用,這里不再講述,網上已有分析,鏈接如下: layout_weight的深刻理解

        二、RelativeLayout
        RelativeLayout的注釋: /*** A Layout where the positions of the children can be described in relation to each other or to the* parent.*/ //在RelativeLayout中,可以依據一個子布局(控件)與其他子布局(控件) //或者父窗體的相對位置關系來描述它的位置 public class RelativeLayout extends ViewGroup {} 在設置Layout里邊的控件的位置時,RelativeLayout中的控件可以使用的屬性有:
        A、設置該控件和父控件的相對位置(屬性值為true或false):
        android:layout_centerHrizontal水平居中
        android:layout_centerVertical垂直居中
        android:layout_centerInparent相對于父元素完全居中
        android:layout_alignParentBottom貼緊父元素的下邊緣
        android:layout_alignParentLeft貼緊父元素的左邊緣
        android:layout_alignParentRight貼緊父元素的右邊緣
        android:layout_alignParentTop貼緊父元素的上邊緣

        B、設置該控件和某個兄弟控件的相對位置(屬性值為控件的id):
        android:layout_below在某元素的下方
        android:layout_above在某元素的的上方
        android:layout_toLeftOf在某元素的左邊
        android:layout_toRightOf在某元素的右邊
        android:layout_alignBaseline本元素的baseline和給定元素的baseline對齊
        android:layout_alignTop本元素的上邊緣和某元素的的上邊緣對齊
        android:layout_alignLeft本元素的左邊緣和某元素的的左邊緣對齊
        android:layout_alignBottom本元素的下邊緣和某元素的的下邊緣對齊
        android:layout_alignRight本元素的右邊緣和某元素的的右邊緣對齊

        C、設置該控件距左、上、右、下邊(無論是父控件還是兄弟控件)的長度(屬性值為具體值如30dip,在LinearLayout中也適用):
        android:layout_marginLeft設置該控件距左邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginTop設置該控件距上邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginRight設置該控件距右邊(無論是父控件還是兄弟控件)的長度
        android:layout_marginBottom設置該控件距下邊(無論是父控件還是兄弟控件)的長度



        ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?下篇 ? ?確定控件的其他屬性
        以下為各個控件的常用屬性總結:

        (待補充)

        轉載于:https://www.cnblogs.com/hwgt/p/5414401.html

        總結

        以上是生活随笔為你收集整理的Android - 布局详解之LinearLayout与RelativeLayout的全部內容,希望文章能夠幫你解決所遇到的問題。

        如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。