Android布局中g(shù)ravity與layout_gravity屬性說明
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="我是測(cè)試內(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="我是測(cè)試內(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="我是測(cè)試內(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="我是測(cè)試內(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="我是測(cè)試內(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 兩個(gè)ViewPager的聯(lián)動(dòng)效果的實(shí)現(xiàn)
這篇文章主要介紹了Android 兩個(gè)ViewPager的聯(lián)動(dòng)效果的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Android CameraX結(jié)合LibYUV和GPUImage自定義相機(jī)濾鏡
之前使用Camera實(shí)現(xiàn)了一個(gè)自定義相機(jī)濾鏡(Android自定義相機(jī)濾鏡 ),但是運(yùn)行起來有點(diǎn)卡頓,這次用Camerax來實(shí)現(xiàn)一樣的效果發(fā)現(xiàn)很流暢,在此記錄一下,也希望能幫到有需要的同學(xué)2021-12-12解決Android 5.1限制外置SD卡寫入權(quán)限的問題
今天小編就為大家分享一篇解決Android 5.1限制外置SD卡寫入權(quán)限的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Flutter?Widget之NavigationBar使用詳解
這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android以對(duì)話框形式制作數(shù)字軟鍵盤示例
大家好,本篇文章主要講的是Android以對(duì)話框形式制作數(shù)字軟鍵盤示例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android使用fragment實(shí)現(xiàn)左側(cè)導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02