最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android studio六大基本布局詳解

 更新時間:2023年04月03日 17:12:50   作者:AaVictory.  
這篇文章主要介紹了Android常用的布局方式:線性布局,相對布局,表格布局,層布局,絕對布局,網(wǎng)格布局,用的相對較多的是線性布局和相對布局。感興趣的同學(xué)可以參考閱讀

Android中常用的布局方式有以下幾種:

  • 線性布局LinearLayout

  • 相對布局RelativeLayout

  • 表格布局TableLayout

  • 層布局FrameLayout

  • 絕對布局AbsoluteLayout

  • 網(wǎng)格布局GridLayout

 用的相對較多的是線性布局和相對布局。接下來重點演示這兩種布局
其中,表格布局是線性布局的子類。網(wǎng)格布局是android 4.0后新增的布局。

(一)線性布局LinearLayout

線性布局中最重要的屬性:orientation
horizontal(水平布局)和vertical(垂直布局)兩種方式

屬性名

  • orientation 布局方式,有horizontal(水平布局)和vertical(垂直布局)兩種方式
  • id 組件名稱
  • layout_width 該組件的寬度
  • layout_height 該組件的高度
  • layout_weight 權(quán)重
  • layout_gravity 該組件(在父容器)中的對齊方式
  • gravity 該組件所含子組件在其內(nèi)部的對齊方式
  • background 設(shè)置背景圖片或填充顏色

效果圖

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/gray"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:text="權(quán)重1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="權(quán)重2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="權(quán)重3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="權(quán)重4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <TextView
            android:text="權(quán)重5"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="20dp"
        android:background="@color/teal_200"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第一個布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:background="@color/purple"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第二個布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:background="@color/teal"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:text="第三個布局"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</LinearLayout>

(二)相對布局RelativeLayout

屬性:

  • android:layout_marginTop=“25dip” //頂部距離
  • android:gravity=“left” //空間布局位置
  • android:layout_marginLeft="15dip //距離左邊距

相對于給定ID控件

  • android:layout_above 將該控件的底部置于給定ID的控件之上;
  • android:layout_below 將該控件的底部置于給定ID的控件之下;
  • android:layout_toLeftOf 將該控件的右邊緣與給定ID的控件左邊緣對齊;
  • android:layout_toRightOf 將該控件的左邊緣與給定ID的控件右邊緣對齊;
  • android:layout_alignBaseline 將該控件的baseline與給定ID的baseline對齊;
  • android:layout_alignTop 將該控件的頂部邊緣與給定ID的頂部邊緣對齊;
  • android:layout_alignBottom 將該控件的底部邊緣與給定ID的底部邊緣對齊;
  • android:layout_alignLeft 將該控件的左邊緣與給定ID的左邊緣對齊;
  • android:layout_alignRight 將該控件的右邊緣與給定ID的右邊緣對齊;

相對于父組件

  • android:layout_alignParentTop 如果為true,將該控件的頂部與其父控件的頂部對齊;
  • android:layout_alignParentBottom 如果為true,將該控件的底部與其父控件的底部對齊;
  • android:layout_alignParentLeft 如果為true,將該控件的左部與其父控件的左部對齊;
  • android:layout_alignParentRight 如果為true,將該控件的右部與其父控件的右部對齊;

居中

  • android:layout_centerHorizontal 如果為true,將該控件的置于水平居中;
  • android:layout_centerVertical 如果為true,將該控件的置于垂直居中;
  • android:layout_centerInParent 如果為true,將該控件的置于父控件的中央;

指定移動像素

  • android:layout_marginTop 上偏移的值;
  • android:layout_marginBottom 下偏移的值;
  • android:layout_marginLeft   左偏移的值;
  • android:layout_marginRight   右偏移的值;

效果圖

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/gray"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:gravity="center"
        android:background="@color/teal"
        android:text="text1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:id="@+id/tv_two"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_centerInParent="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
    <TextView
        android:layout_above="@+id/tv_two"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:background="@color/teal"
        android:text="text4"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />
</RelativeLayout>

(三)表格布局TableLayout

屬性

三個常用屬性

  • android:collapseColumns:設(shè)置需要被隱藏的列的序號
  • android:shrinkColumns:設(shè)置允許被收縮的列的列序號
  • android:stretchColumns:設(shè)置運行被拉伸的列的列序號

(四)幀布局FrameLayout

FrameLayout(幀布局)可以說是六大布局中最為簡單的一個布局,這個布局直接在屏幕上開辟出一塊空白的區(qū)域,當我們往里面添加控件的時候,會默認把他們放到這塊區(qū)域的左上角,而這種布局方式卻沒有任何的定位方式,所以它應(yīng)用的場景并不多;幀布局的大小由控件中最大的子控件決定,如果控件的大小一樣大的話,那么同一時刻就只能看到最上面的那個組件!后續(xù)添加的控件會覆蓋前一個!雖然默認會將控件放置在左上角,但是我們也可以通過layout_gravity屬性,指定到其他的位置!

效果圖

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/gray"
    android:layout_height="match_parent">
    <TextView
        android:background="#000000"
        android:layout_width="fill_parent"
        android:layout_height="180dp"/>
    <TextView
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="130dp"/>
    <TextView
        android:background="#ff00ff"
        android:layout_width="fill_parent"
        android:layout_height="100dp"/>
    <TextView
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_height="50dp"/>
</FrameLayout>

(五)絕對布局AbsoluteLayout

屬性:

  • 絕對布局又可以叫做坐標布局,可以直接指定子元素的絕對位置(xy)
  • 由于手機屏幕尺寸差別比較大使用絕對定位的適應(yīng)性會比較差,在屏幕的適配上有缺陷

常用屬性:

  • android:foreground:*設(shè)置改幀布局容器的前景圖像
  • android:foregroundGravity:設(shè)置前景圖像顯示的位置
  • android:layout_x=”” 控制當前子類控件的x位置
  • android:layout_y=”” 控制當前子類控件的y位置

效果圖

.xml布局

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

和之前的TableLayout(表格布局) 有點類似,不過網(wǎng)格布局的好處是:

  • 可以自己設(shè)置布局中組件的排列方式
  • 可以自定義網(wǎng)格布局有多少行,多少列
  • 可以直接設(shè)置組件位于某行某列
  • 可以設(shè)置組件橫跨幾行或者幾列

效果圖

.xml布局:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:orientation="horizontal"
    android:rowCount="6" >
    <TextView
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#15CBE3"
        android:text="0"
        android:textSize="50sp" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="回退" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="清空" />

    <Button android:text="1" />

    <Button android:text="2" />

    <Button android:text="3" />
    <Button android:text="+" />


    <Button android:text="4" />

    <Button android:text="5" />

    <Button android:text="6" />
    <Button android:text="-" />


    <Button android:text="7" />

    <Button android:text="8" />

    <Button android:text="9" />
    <Button android:text="*" />


    <Button android:text="0" />
    <Button android:text="." />

    <Button android:text="=" />
    <Button android:text="/" />
</GridLayout>

<GridLayout android:layout_width=“fill_parent”:網(wǎng)格布局寬度為填滿屏幕

<GridLayout android:layout_height=“wrap_content”:網(wǎng)格布局高度為包裹內(nèi)容

<GridLayout android:columnCount=“4”:網(wǎng)格布局設(shè)置 4 列

<GridLayout android:rowCount=“6”:網(wǎng)格布局設(shè)置 6 行

<GridLayout android:layout_columnSpan=“2”:清空和回退橫跨兩列

<GridLayout android:orientation=“horizontal”:網(wǎng)格布局設(shè)置為水平布局

以上就是Android studio六大基本布局詳解的詳細內(nèi)容,更多關(guān)于Android studio基本布局的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android MediaPlayer 播放音頻的方式

    Android MediaPlayer 播放音頻的方式

    這篇文章主要介紹了Android MediaPlayer 播放音頻的方式,本文給大家詳細介紹了MediaPlayer的使用方式,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法

    Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法

    這篇文章主要介紹了Android Studio升級3.6 Build窗口出現(xiàn)中文亂碼問題解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Android中使用AspectJ詳解

    Android中使用AspectJ詳解

    本文主要介紹了Android中使用AspectJ的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法

    Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法

    下面小編就為大家分享一篇Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android中監(jiān)聽短信的兩種方法

    Android中監(jiān)聽短信的兩種方法

    這篇文章主要介紹了Android中監(jiān)聽短信的兩種方法,本文講解了監(jiān)聽廣播、采用觀察方法,監(jiān)聽短信數(shù)據(jù)庫兩種方法,需要的朋友可以參考下
    2015-04-04
  • Android雷達掃描動態(tài)界面制作

    Android雷達掃描動態(tài)界面制作

    這篇文章主要為大家詳細介紹了Android雷達掃描動態(tài)界面制作資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android 單例模式實現(xiàn)可復(fù)用數(shù)據(jù)存儲的詳細過程

    Android 單例模式實現(xiàn)可復(fù)用數(shù)據(jù)存儲的詳細過程

    本文介紹了如何使用單例模式實現(xiàn)一個可復(fù)用的數(shù)據(jù)存儲類,該類可以存儲不同類型的數(shù)據(jù),并提供統(tǒng)一的接口來訪問這些數(shù)據(jù),通過雙重檢查鎖定機制,該類在多線程環(huán)境下是線程安全的,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • Android Service中使用Toast無法正常顯示問題的解決方法

    Android Service中使用Toast無法正常顯示問題的解決方法

    這篇文章主要介紹了Android Service中使用Toast無法正常顯示問題的解決方法,分析了Service中Toast無法正常顯示的原因與相關(guān)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • Flutter 常用插件匯總

    Flutter 常用插件匯總

    Flutter 有了谷歌強大后盾加持,加上跨平臺的特性,生態(tài)日益豐富,目前大部分應(yīng)用能夠用到的插件在 pub.flutter-io.cn上都可以找得到。本篇介紹 Flutter 最為常見的插件,以避免重復(fù)造輪子和新手少走彎路。
    2021-05-05
  • Android中獲取資源 id 及資源 id 的動態(tài)獲取

    Android中獲取資源 id 及資源 id 的動態(tài)獲取

    這篇文章主要介紹了 Android中獲取資源 id 及資源 id 的動態(tài)獲取的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評論

罗山县| 凉城县| 托克逊县| 安溪县| 田林县| 正阳县| 义马市| 平潭县| 阳东县| 政和县| 辽宁省| 杭锦后旗| 河北区| 松江区| 峨眉山市| 安平县| 三门县| 黄梅县| 南投县| 新乡市| 都兰县| 金秀| 广丰县| 西平县| 邹平县| 平远县| 三穗县| 巴青县| 兴文县| 吐鲁番市| 会同县| 黄冈市| 安仁县| 安丘市| 绥滨县| 平乡县| 丹巴县| 晋中市| 怀安县| 乌鲁木齐市| 赤水市|