欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android布局中gravity與layout_gravity屬性說明

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

gravity與layout_gravity屬性

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

但是gravity有不同的類型:

  • gravity
  • layout_gravity
  • 相對布局中的layout_center等屬性

今天我們就來具體說說。

1、gravity

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

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

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

2、layout_gravity屬性

與gravity屬性不同的是,layout_gravity屬性是用來設置該View相對與父View的位置,

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

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

我直接在一個布局中,把這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這一屬性根本沒有起到作用,個人看法是子控件如果選擇橫或縱居中,這種屬性聲明是不能和父布局的排列方式相沖的,這個坑我先替你們踩著:D。

3、相對布局中的layout_center屬性

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

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

總結

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

相關文章

  • Android 兩個ViewPager的聯(lián)動效果的實現(xiàn)

    Android 兩個ViewPager的聯(lián)動效果的實現(xiàn)

    這篇文章主要介紹了Android 兩個ViewPager的聯(lián)動效果的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android 畫中畫模式的實現(xiàn)示例

    Android 畫中畫模式的實現(xiàn)示例

    這篇文章主要介紹了Android 畫中畫模式的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Android CameraX結合LibYUV和GPUImage自定義相機濾鏡

    Android CameraX結合LibYUV和GPUImage自定義相機濾鏡

    之前使用Camera實現(xiàn)了一個自定義相機濾鏡(Android自定義相機濾鏡 ),但是運行起來有點卡頓,這次用Camerax來實現(xiàn)一樣的效果發(fā)現(xiàn)很流暢,在此記錄一下,也希望能幫到有需要的同學
    2021-12-12
  • 解決Android 5.1限制外置SD卡寫入權限的問題

    解決Android 5.1限制外置SD卡寫入權限的問題

    今天小編就為大家分享一篇解決Android 5.1限制外置SD卡寫入權限的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Flutter?Widget之NavigationBar使用詳解

    Flutter?Widget之NavigationBar使用詳解

    這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android表格自定義控件使用詳解

    Android表格自定義控件使用詳解

    這篇文章主要為大家詳細介紹了Android表格自定義控件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android Zxing二維碼掃描圖片拉伸問題的解決方法

    Android Zxing二維碼掃描圖片拉伸問題的解決方法

    這篇文章主要為大家詳細介紹了Android Zxing二維碼掃描圖片拉伸問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android以對話框形式制作數(shù)字軟鍵盤示例

    Android以對話框形式制作數(shù)字軟鍵盤示例

    大家好,本篇文章主要講的是Android以對話框形式制作數(shù)字軟鍵盤示例,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Android使用fragment實現(xiàn)左側導航

    Android使用fragment實現(xiàn)左側導航

    這篇文章主要為大家詳細介紹了Android使用fragment實現(xiàn)左側導航,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 利用Flutter制作一個摸魚桌面版App

    利用Flutter制作一個摸魚桌面版App

    Win10商店上架了一款名為《摸魚》的App,在下載打開之后,這個App會讓你的電腦進入一個假更新的畫面。本文將為大家介紹如何通過Flutter制作一個桌面版的摸魚APP,快跟小編一起學習一下吧
    2021-12-12

最新評論