日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android layout_gravity 和 gravity的区别

發布時間:2024/3/12 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android layout_gravity 和 gravity的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????這兩個屬性,有時候蠻容易混淆,好記性不如爛筆頭 ,還是直接記錄下來吧

1. android:layout_gravity定義:

android:layout_gravityGravity specifies how a component should be placed in its group of cells.

?android:layout_gravity 是指UI自身控件, 放在父布局中的哪個位置, 舉個例子:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="300dp" tools:context=".MainActivity"><Buttonandroid:layout_width="70dp"android:background="#992288"android:layout_height="50dp"android:layout_gravity="bottom" # 把按鈕UI控件,放到父布局LinearLayout的底部位置android:text="點擊按鈕"/></LinearLayout>

2. android:gravity定義:?

android:gravity

.Specifies how an object should position its content,

?on both the X and Y axes, within its own bounds.

android:gravity? 是指UI控件里面的元素,放在UI控件里面的哪個位置?舉個例子:一個TextView 控件上的“hello?world”字符串,設置android:gravity=“right”,那么“hello world”會顯示在TextView的右邊

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="300dp" tools:context=".MainActivity"><TextViewandroid:layout_width="180dp"android:background="#992288"android:layout_height="50dp"android:gravity="right" #設置hello world字符串 在 TextView UI控件的右邊android:text="hello world"/></LinearLayout>

效果圖如下:

?3. 常用屬性值:

Constant

? ? ? ? ? ? ? ? ? ? ? ? ? Description

bottom

將對象放在其容器的底部,而不改變其大小。

center

將對象放在其容器的垂直和水平軸中心,不改變其大小。

center_horizontal

將對象放在其容器的水平中心,不改變其大小。

center_vertical

將對象放在其容器的垂直中心,不改變其大小。

end

將對象其容器的末尾,而不改變其大小。

left

將對象放到其容器的左側,而不改變其大小。

right

將對象到其容器的側,而不改變其大小。

start

將對象到其容器的起始位置,而不改變其大小。

top

將對象放到其容器的頂部,而不改變其大小。

clip_horizontal

附加選項,用于按照容器的邊來剪切對象的頂部和/或底部的內容. 剪切基于其縱向對齊設置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.

垂直方向裁剪

clip_vertical

附加選項,用于按照容器的邊來剪切對象的左側和/或右側的內容. 剪切基于其橫向對齊設置:左側對齊時,剪切右側;右側對齊時剪切左側;除此之外剪切左側和右側.

水平方向裁剪

fill

如果需要,增加對象的水平和垂直大小,使其完全填滿其容器.

fill_horizontal

如果需要,增加對象的水平尺寸,使其完全填滿其容器。

fill_vertical

如果需要,增加對象的垂直尺寸,使其完全填滿其容器。


4. 使用注意事項

? ? ? ? 第一:android:layout_gravity 是LinearLayout 線性布局中的屬性,在RelativeLayout?相對布

局中設置不會生效。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="300dp"tools:context=".MainActivity"><TextViewandroid:id="@+id/test_textview"android:layout_width="180dp"android:layout_height="50dp"android:layout_gravity="right" <!-- android:layout_gravity 是LinearLayout中的屬性,所以在RelativeLayout相對布局中設置不生效, 所以TextView 不會居右顯示-->android:background="#992288"android:text="hello world" /><Buttonandroid:layout_width="100dp"android:layout_height="50dp"android:layout_below="@id/test_textview"android:layout_marginTop="20dp"android:background="@color/colorPrimary"android:text="按鈕點擊"android:gravity="right" <!--但是 android:gravity 也是RelativeLayout相對布局中的屬性,所以這里設置是有效的-->android:textSize="12sp" /> </RelativeLayout> 效果圖如下:

? ? ? ? ?第二: 當采用LinearLayout線性布局作為父布局,

? ? 1. 如果設置?android:orientation="horizontal" :? 元素布局排列為水平方向

這時android:layout_gravity 只有垂直方向的設置屬性才會生效,而水平方向的設置屬性不生效

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="300dp"android:orientation="horizontal"tools:context=".MainActivity"><TextViewandroid:id="@+id/test_textview"android:layout_width="180dp"android:layout_height="50dp"android:layout_gravity="right"android:background="#992288"android:text="hello world" /><!--由于設置的android:orientation="horizontal",android:layout_gravity在設置水平方向的屬性值 比如 left start right end 不會生效-這里我設置為right,預想會顯示在右邊,但實際上顯示在左側,不生效--><Buttonandroid:layout_width="100dp"android:layout_height="50dp"android:layout_marginTop="80dp"android:background="@color/colorPrimary"android:text="按鈕點擊"android:layout_gravity="bottom"android:textSize="12sp" /><!--由于設置的android:orientation="horizontal",android:layout_gravity在設置垂直平方向的屬性值 比如 top bottom center_vertical 會生效--></LinearLayout >

?效果圖:

?2. 如果設置?android:orientation="virtical" : 元素布局排列為垂直方向

這時android:layout_gravity 只有水平方向的設置屬性才會生效,而垂直方向的設置屬性不生效

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="300dp"android:orientation="vertical" tools:context=".MainActivity"><TextViewandroid:id="@+id/test_textview"android:layout_width="180dp"android:layout_height="50dp"android:layout_gravity="bottom"android:background="#992288"android:text="hello world" /><!--由于設置的android:orientation="virtical",android:layout_gravity在設置垂直方向的屬性值 比如 top bottom center_vertical不會生效雖然我設置的是bottom,預想顯示在底部,實際上不生效--><Buttonandroid:layout_width="100dp"android:layout_height="50dp"android:layout_gravity="left"android:layout_marginTop="80dp"android:background="@color/colorPrimary"android:text="按鈕點擊"android:textSize="12sp" /><!--由于設置的android:orientation="virtical",android:layout_gravity在設置水平平方向的屬性值 比如 start left right end 會生效--></LinearLayout>

效果圖:

總結

以上是生活随笔為你收集整理的Android layout_gravity 和 gravity的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。