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

Android布局中g(shù)ravity與layout_gravity屬性說明

 更新時(shí)間:2023年01月17日 09:50:35   作者:黃元帥  
這篇文章主要介紹了Android布局中g(shù)ravity與layout_gravity屬性說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

gravity與layout_gravity屬性

在android布局中,我們經(jīng)常會(huì)用到“重心”-gravity這個(gè)屬性。

但是gravity有不同的類型:

  • gravity
  • layout_gravity
  • 相對(duì)布局中的layout_center等屬性

今天我們就來具體說說。

1、gravity

gravity屬性是對(duì)控件自身內(nèi)容對(duì)自己的限定,拿布局文件test.xml舉例來說:

此時(shí)在TextView中并沒有對(duì)gravity屬性進(jìn)行操作,文字內(nèi)容如上圖。

接下來,我們繼續(xù)設(shè)置TextView的gravity屬性,觀察效果:

2、layout_gravity屬性

與gravity屬性不同的是,layout_gravity屬性是用來設(shè)置該View相對(duì)與父View的位置,

具體情況就個(gè)人判斷有下面這4種情況:

另外還有一種在父布局橫或縱設(shè)置wrap_content時(shí),如果在該方向設(shè)置layout_gravity屬性。

我直接在一個(gè)布局中,把這5種情況列出來,下面是我的布局文件代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.primexiao.myapplication.MainActivity"
    >
//第4種情況
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#00f0f0"
    android:orientation="vertical">
        <TextView
            android:id="@+id/tv1"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:text="我是測試內(nèi)容"
            android:background="#000000"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff"
            android:layout_width="100dp"
            android:layout_height="100dp" />
</LinearLayout>
//第1種情況
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00"
        android:orientation="vertical">
        <TextView
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:text="我是測試內(nèi)容"
            android:background="#000000"
            android:textColor="#ffffff"
            android:layout_gravity="center_vertical"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>
    //第3種情況
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00f0f0"
        android:orientation="horizontal">
        <TextView
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:text="我是測試內(nèi)容"
            android:background="#000000"
            android:layout_gravity="center_vertical"
            android:textColor="#ffffff"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>
    //第2種情況
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:orientation="horizontal">
        <TextView
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:text="我是測試內(nèi)容"
            android:background="#000000"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>
    //第5種情況
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fff000"
        android:orientation="horizontal">
        <TextView
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"
            android:text="我是測試內(nèi)容"
            android:background="#000000"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>
</LinearLayout>

效果圖如下:

我們可以看到第1和第2種情況下,layout_gravity這一屬性根本沒有起到作用,個(gè)人看法是子控件如果選擇橫或縱居中,這種屬性聲明是不能和父布局的排列方式相沖的,這個(gè)坑我先替你們踩著:D。

3、相對(duì)布局中的layout_center屬性

之前遇到過這么一個(gè)問題,在RelativeLayout中設(shè)置layount_gravity屬性,發(fā)現(xiàn)并不能實(shí)現(xiàn)居中效果,并且layout_gravity也是手動(dòng)輸入,期間并沒有智能提示。

后來發(fā)現(xiàn)相對(duì)布局中,有l(wèi)ayout_centerX這么一個(gè)屬性,讓我們來試一下:

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?中的監(jiān)聽和按鍵處理詳情

    Android?中的監(jiān)聽和按鍵處理詳情

    這篇文章主要介紹了Android?中的監(jiān)聽和按鍵處理詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式的兩種方法

    使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式的兩種方法

    這篇文章主要介紹了使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式,兩種方式通過命令給大家寫的非常詳細(xì),文中又給大家補(bǔ)充介紹了高通手機(jī)?進(jìn)入?高通9008模式的兩種方法,需要的朋友可以參考下
    2023-01-01
  • Android動(dòng)畫系列之屬性動(dòng)畫的基本使用教程

    Android動(dòng)畫系列之屬性動(dòng)畫的基本使用教程

    這篇文章主要給大家介紹了關(guān)于Android動(dòng)畫系列教程之屬性動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Android ListView滑動(dòng)刪除操作(SwipeListView)

    Android ListView滑動(dòng)刪除操作(SwipeListView)

    這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)刪除操作,主要是學(xué)習(xí)SwipeListView開源框架。感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android自定義彈出框dialog效果

    Android自定義彈出框dialog效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義彈出框dialog效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 淺談Android截屏和指定View生成截圖

    淺談Android截屏和指定View生成截圖

    本文主要介紹了Android如何截屏和指定View生成截圖,感興趣的同學(xué),可以參考下
    2021-06-06
  • 故事講解Activity生命周期(貓的一生)

    故事講解Activity生命周期(貓的一生)

    關(guān)于Android中Activity的生命周期,網(wǎng)上大多數(shù)文章基本都是直接貼圖、翻譯API,比較籠統(tǒng)含糊不清。本文主要用故事講解Activity生命周期。下面跟著小編一起來看下吧
    2017-03-03
  • Kotlin ViewModelProvider.Factory的使用實(shí)例詳解

    Kotlin ViewModelProvider.Factory的使用實(shí)例詳解

    這篇文章主要介紹了Kotlin ViewModelProvider.Factory的使用,在我們使用 ViewModel 的時(shí)候,我們會(huì)發(fā)現(xiàn),有的時(shí)候我們需要用到 ViewModelFactory,有的時(shí)候不需要
    2023-02-02
  • Android inflater 用法及不同點(diǎn)

    Android inflater 用法及不同點(diǎn)

    在 實(shí)際開發(fā)中LayoutInflater這個(gè)類還是非常有用的,它的作用類似于findViewById()。這篇文章主要介紹了Android inflater 用法,需要的朋友可以參考下
    2018-11-11
  • android照相、相冊(cè)獲取圖片剪裁報(bào)錯(cuò)的解決方法

    android照相、相冊(cè)獲取圖片剪裁報(bào)錯(cuò)的解決方法

    最近在項(xiàng)目中用到了照相和相冊(cè)取圖剪裁上傳頭像,就在網(wǎng)上逛了逛,基本都是千篇一律,就弄下來用了用,沒想到的是各種各樣的奇葩問題就出現(xiàn)了。先給大家看看代碼問題慢慢來解決
    2014-11-11

最新評(píng)論

新龙县| 澜沧| 和硕县| 乌拉特中旗| 黎平县| 威信县| 德兴市| 民乐县| 沈阳市| 礼泉县| 香港 | 双江| 赞皇县| 绩溪县| 拉孜县| 定结县| 德格县| 乳山市| 大安市| 吉木乃县| 岳阳市| 锦州市| 彭州市| 澜沧| 湟中县| 临江市| 本溪| 莱芜市| 潜江市| 大关县| 远安县| 合水县| 崇文区| 乌拉特中旗| 永顺县| 吴川市| 绥棱县| 临海市| 大安市| 富蕴县| 永登县|