gravity、layout_gravity及orientation
gravity、layout_gravity及orientation
?
最近在弄一個簡單的界面:橫向,添加一張準備好的背景圖,在界面右邊居中放置一個按鈕。實現過程中發現對布局的主要屬性沒有想象中地那么熟悉,又重新找資料樹梳理了一遍,這里總結一下。
?
?????? 1、gravity、layout_gravity及orientation基本概念
gravity,中文意思為重心(理科生不會陌生吧),表示組件(View)橫向和縱向的停靠位置。如果不進行任何設置,默認是靠左。
android:gravity,對組件本身來說的,作用是設置表示組件包含的內容顯示在表示組件的什么位置,默認值為左側。最常見的例子就是組件上的文本,如android:gravity=”center”,那么文本顯示位置為垂直和水平方向均居中。
android:layout_gravity,相對于包含該組件(元素)的父組件來說的,設置該組件在父組件的顯示位置。若android:layout_gravity="center_vertical",那么該組件在父組件中的位置為垂直方向居中,但水平方向可能是靠左的。orientation,線性布局時以列或行來顯示內部子元素,注意在其他布局一般用不到該屬性,如AbsoluteLayout等。該屬性默認是水平排列(horizontal),即以行的形式來布置包含的子組件,在實際應用程序的開發中用得較多是垂直排布,即不一定要進行如下設定:android:orientation=”vertical”。
?
2、gravity與layout_gravity屬性介紹
top,Put the object at the top of its container, not changing its size。將對象放在其容器的頂部,不改變其大小。
bottom,Put the object at the bottom of its container, not changing its size。將對象放在其容器的底部,不改變其大小。
left,Put the object at the left edge of its container, not changing its size。將對象放在其容器的左側,不改變其大小。
right ,Put the object at the right edge of its container, not changing its size。將對象放在其容器的右側,不改變其大小。
center_vertical,Place object in the vertical center of its container, not changing its size。將對象縱向居中,不改變其大小。垂直對齊方式:垂直方向上居中對齊。
fill_vertical,Grow the vertical size of the object if needed so it completely fills its container。必要的時候增加對象的縱向大小,以完全充滿其容器。垂直方向填充。
center_horizontal,Place object in the horizontal center of its container, not changing its size。將對象橫向居中,不改變其大小。水平對齊方式:水平方向上居中對齊
fill_horizontal, Grow the horizontal size of the object if needed so it completely fills its container。必要的時候增加對象的橫向大小,以完全充滿其容器。
center,Place the object in the center of its container in both the vertical and horizontal axis, not changing its size。將對象橫縱居中,不改變其大小。
fill,Grow the horizontal and vertical size of the object if needed so it completely fills its container。This is the default。必要的時候增加對象的橫縱向大小,以完全充滿其容器。水平方向填充。
clip_vertical,Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds。 The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges。附加選項,用于按照容器的邊來剪切對象的頂部和/或底部的內容。 剪切基于其縱向對齊設置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部。垂直方向裁剪。
clip_horizontal,Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds。 The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges。附加選項,用于按照容器的邊來剪切對象的左側和/或右側的內容。 剪切基于其橫向對齊設置:左側對齊時,剪切右側;右側對齊時剪切左側;除此之外剪切左側和右側。水平方向裁剪。
?
3、xml代碼實現
界面的橫向設置與背景圖的添加很容易實現,針對按鈕靠右居中,剛開始沒有想到利用orientation這個屬性,想當然地以為layout_gravity能夠搞定,所以過程有點波折。
整體設計思路為:最外層放置一個LinearLayout,設置子組件的布局為垂直方向居中并靠右,代碼如下:
1 android:layout_width="match_parent" 2 android:layout_height="match_parent" 3 android:gravity="center_vertical|right" 4 android:background="@drawable/login_bg"里層再放置一個LinearLayout,用來放置具體的按鈕。通過上一步的設置,該LinearLayout顯示在了界面的右邊,并且是垂直方向居中(其實一般應用中是讓該LinearLayout的layout_height屬性為match_parent,故父組件中gravity屬性的center_vertical值可以不添加)。
到這里,完成了大半,接下來就是添加按鈕。注意,目標是讓按鈕在界面上靠右并居中。剛開始將內層LinearLayout的gravity屬性設置為垂直居中,縱向鋪滿屏幕,為了美觀,讓其距界面右邊界40個像素無關點。代碼如下:
1 android:layout_width="wrap_content" 2 android:layout_height="match_parent" 3 android:gravity="center_vertical" 4 android:layout_marginRight="@dimen/margin_Right"但是問題來了,添加三個按鈕,以為能夠達到預想的效果:水平與垂直方向上均居中地排布在內層的LinearLayout中,并且第二個在第一個下面,第三個在第二個下面。然而,現實是這樣的:
后面看了一些例子,恍然大悟,子組件的縱向或橫向上的排布,需要借助orientation屬性。馬上加入代碼:
android:orientation="vertical"
滿意的結果出現了:
三個按鈕的實現代碼如下:
1 <Button android:id="@+id/button1" 2 android:layout_height="wrap_content" 3 android:layout_width="wrap_content" 4 android:text="@string/login_by_weixin" 5 android:textColor="@string/color_weixin" 6 android:textSize="@dimen/margin_Right" /> 7 8 <Button android:id="@+id/button2" 9 android:layout_height="wrap_content" 10 android:layout_width="wrap_content" 11 android:layout_marginTop="@dimen/activity_vertical_margin" 12 android:text="@string/logout_by_weixin" 13 android:textColor="@string/color_weixin" 14 android:textSize="@dimen/margin_Right" /> 15 16 <Button android:id="@+id/button3" 17 android:layout_height="wrap_content" 18 android:layout_width="wrap_content" 19 android:layout_marginTop="@dimen/activity_vertical_margin" 20 android:text="@string/close" 21 android:textColor="@string/color_weixin" 22 android:textSize="@dimen/margin_Right" />代碼中對按鈕的大小、具體位置、文本(顏色、大小)進行了設置。
可以發現,若僅將LinearLayout的gravity屬性設置為垂直居中,那么當子組件個數大于1時,會全部排列在居中位置,放不下只會在水平方向上錯位顯示,并不會從上到下依次排布,此時就需要借助orientation屬性了。
轉載于:https://www.cnblogs.com/tgyf/p/4726413.html
總結
以上是生活随笔為你收集整理的gravity、layout_gravity及orientation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Xamarin开发 Android 系
- 下一篇: junit基础学习之-断言注解(3)