亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android四種常見布局方式示例教程

 更新時間:2022年09月28日 11:35:42   作者:Shewyoo  
Android四種布局有線性布局LinearLayout、相對布局RelativeLayout、網(wǎng)格布局GridLayout、和滾動視圖ScrollView,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

一、線性布局LinearLayout

有兩種排序方式

  • orientation屬性值為horizontal時,內(nèi)部視圖在水平方向從左往右排列。
  • orientation屬性值為vertical時,內(nèi)部視圖在垂直方向從上往下排列。

如果不指定orientation屬性,則LinearLayout默認水平方向排列。

線性布局的權(quán)重

指線性布局的下級視圖各自擁有多大比例的寬高。

屬性名為layout_weight,但該屬性不在LinearLayout節(jié)點設置,而在線性布局的直接下級視圖設置,表示改下級視圖占據(jù)的寬高比例。

  • layout_width為0dp時,表示水平方向的寬度比例
  • layout_height為0dp時,表示垂直方向的高度比例

例:

第一個線性布局:width = 0dp 說明在水平方向設置寬度比例,weight = 1,占據(jù)weight總數(shù)的1/2,則占據(jù)一半空間。

第二個線性布局:height = 0dp 說明在垂直方向設置寬度比例,weight = 1,占據(jù)weight總數(shù)的1/3,則占據(jù)三分之一空間。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"http://寬度為0dp,通過權(quán)重設置寬度比例
            android:layout_height="wrap_content"
            android:layout_weight="1"http://weight為1,下面的weight也為1,占1/2,即寬度比例占1/2
            android:text="橫排第一個"
            android:textSize="17sp"
            android:textColor="#000000"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="橫排第二個"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"http://高度為0dp,通過權(quán)重設置高度比例
            android:layout_weight="1"http://weight為1,下面的weight為2,占1/3,即寬度比例占1/3
            android:text="豎排第一個"
            android:textSize="17sp"
            android:textColor="#000000"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="豎排第二個"
            android:textSize="17sp"
            android:textColor="#000000"/>
    </LinearLayout>

二、相對布局RelativeLayout

相對布局的視圖位置由平級或上級視圖決定,用于確定下級視圖位置的參考物分兩種:

  • 與該視圖自身平級的視圖
  • 該視圖的上級視圖

如果不設定下級視圖的參照物,那么下級視圖默認顯示在RelativeLayout內(nèi)部的左上角。

相對位置的取值

例:

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerInParent="true"
        android:text="中間"
        android:textSize="11sp"
        android:textColor="#000000"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerHorizontal="true"
        android:text="水平中間"
        android:textSize="11sp"
        android:textColor="#000000"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerVertical="true"
        android:text="垂直中間"
        android:textSize="11sp"
        android:textColor="#000000"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_alignParentLeft="true"
        android:text="上級左邊對齊"
        android:textSize="11sp"
        android:textColor="#000000"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_toLeftOf="@id/tv_center"
        android:layout_alignTop="@id/tv_center"
        android:text="中間左邊"
        android:textSize="11sp"
        android:textColor="#000000"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_above="@id/tv_center"
        android:layout_alignLeft="@id/tv_center"
        android:text="中間上邊"
        android:textSize="11sp"
        android:textColor="#000000"/>

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

網(wǎng)格布局支持多行多列的表格排列。

網(wǎng)格布局默認從左往右、從上到下排列,新增兩個屬性:

  • columnCount屬性:指定網(wǎng)格的列數(shù),即每行能放多少視圖。
  • rowCount屬性:指定網(wǎng)格行數(shù),即每列能放多少視圖。

例:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2">
    <TextView
        android:layout_width="0dp"http://設置權(quán)重,占滿屏幕
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#ffcccc"
        android:text="淺紅色"
        android:gravity="center"http://設置文字位于網(wǎng)格中間
        android:textColor="#000000"
        android:textSize="17sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_columnWeight="1"
        android:background="#ffaa00"
        android:text="淺紅色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_columnWeight="1"
        android:background="#00ff00"
        android:text="綠色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_columnWeight="1"
        android:background="#660066"
        android:text="深紫色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>
</GridLayout>

四、滾動視圖ScrollView

有兩種:

  • ScrollView:垂直方向的滾動視圖,垂直方向滾動時,layout_width屬性值設置為match_parent,layout_height 屬性值設置為wrap_content。
  • HorizontalScrollView:水平方向的滾動視圖,水平方向滾動時,layout_width屬性值設置為wrap_content,layout_height屬性值設置為match_parent。

例:

水平方向兩個View共600dp,超出屏幕,所以上級視圖使用HorizontalScrollView,寬度自適應,高度跟隨上級視圖。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">
<!--        水平方向的線性布局-->
        <LinearLayout
            android:layout_width="wrap_content"http://寬度自適應
            android:layout_height="match_parent"http://高度跟隨上級視圖
            android:orientation="horizontal">//水平排列
        <View
            android:layout_width="300dp"http://寬度自定義,超出屏幕
            android:layout_height="match_parent"
            android:background="#aaffff"/>
        <View
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:background="#ffff00"/>
        </LinearLayout>
    </HorizontalScrollView>
<!--        垂直方向的線性布局-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="#aaffff"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="#ffff00"/>
</LinearLayout>
    </ScrollView>
</LinearLayout>

到此這篇關(guān)于Android四種常見布局方式示例教程的文章就介紹到這了,更多相關(guān)Android布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • android實現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識別

    android實現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識別

    這篇文章主要介紹了android實現(xiàn)漢字轉(zhuǎn)拼音功能,帶多音字識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android Studio修改Log信息顏色的實現(xiàn)

    Android Studio修改Log信息顏色的實現(xiàn)

    這篇文章主要介紹了Android Studio修改Log信息顏色的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Android文件下載進度條的實現(xiàn)代碼

    Android文件下載進度條的實現(xiàn)代碼

    我們今天開始學習的是下載進度的實現(xiàn)。今天的這段代碼是網(wǎng)上找的,自己做了些小改,通過模擬器測試。文件下載進度條控制(就是為了高清壁紙加個進度條),自己研究了好久,但是進度條只能顯示緩存寫入文件的進度,不能顯示下載進度。找了好久,終于找到一段用的代碼,所以記錄下來,大家分享
    2013-01-01
  • android開發(fā)教程之使用looper處理消息隊列

    android開發(fā)教程之使用looper處理消息隊列

    這篇文章主要介紹了通過HandlerThread對象來實現(xiàn)使用looper處理消息隊列的功能,大家參考使用吧
    2014-01-01
  • Android 中RecyclerView通用適配器的實現(xiàn)

    Android 中RecyclerView通用適配器的實現(xiàn)

    這篇文章主要介紹了Android 中RecyclerView通用適配器的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android中activity跳轉(zhuǎn)按鈕事件的四種寫法

    Android中activity跳轉(zhuǎn)按鈕事件的四種寫法

    這篇文章主要介紹了Android中activity跳轉(zhuǎn)按鈕事件的四種寫法,下文中包括四個activity的內(nèi)容詳解,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • python只需30行代碼就能記錄鍵盤的一舉一動

    python只需30行代碼就能記錄鍵盤的一舉一動

    這篇文章主要介紹了如何用python只寫30行代碼就能記錄鍵盤的一舉一動,感興趣的同學快來看看吧,新手小白也能掌握
    2021-08-08
  • Android基于高德地圖完全自定義Marker的實現(xiàn)方法

    Android基于高德地圖完全自定義Marker的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android基于高德地圖完全自定義Marker的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • 淺談Android Studio JNI生成so庫

    淺談Android Studio JNI生成so庫

    下面小編就為大家?guī)硪黄獪\談Android Studio JNI生成so庫。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android開發(fā)之加載圖片的方法

    Android開發(fā)之加載圖片的方法

    這篇文章主要介紹了Android開發(fā)之加載圖片的方法,涉及Android圖片操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05

最新評論