gravity、layout_gravity及orientation
gravity、layout_gravity及orientation
?
最近在弄一個(gè)簡單的界面:橫向,添加一張準(zhǔn)備好的背景圖,在界面右邊居中放置一個(gè)按鈕。實(shí)現(xiàn)過程中發(fā)現(xiàn)對布局的主要屬性沒有想象中地那么熟悉,又重新找資料樹梳理了一遍,這里總結(jié)一下。
?
?????? 1、gravity、layout_gravity及orientation基本概念
gravity,中文意思為重心(理科生不會陌生吧),表示組件(View)橫向和縱向的??课恢谩H绻贿M(jìn)行任何設(shè)置,默認(rèn)是靠左。
android:gravity,對組件本身來說的,作用是設(shè)置表示組件包含的內(nèi)容顯示在表示組件的什么位置,默認(rèn)值為左側(cè)。最常見的例子就是組件上的文本,如android:gravity=”center”,那么文本顯示位置為垂直和水平方向均居中。
android:layout_gravity,相對于包含該組件(元素)的父組件來說的,設(shè)置該組件在父組件的顯示位置。若android:layout_gravity="center_vertical",那么該組件在父組件中的位置為垂直方向居中,但水平方向可能是靠左的。orientation,線性布局時(shí)以列或行來顯示內(nèi)部子元素,注意在其他布局一般用不到該屬性,如AbsoluteLayout等。該屬性默認(rèn)是水平排列(horizontal),即以行的形式來布置包含的子組件,在實(shí)際應(yīng)用程序的開發(fā)中用得較多是垂直排布,即不一定要進(jìn)行如下設(shè)定: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。將對象放在其容器的左側(cè),不改變其大小。
right ,Put the object at the right edge of its container, not changing its size。將對象放在其容器的右側(cè),不改變其大小。
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。必要的時(shí)候增加對象的縱向大小,以完全充滿其容器。垂直方向填充。
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。必要的時(shí)候增加對象的橫向大小,以完全充滿其容器。
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。必要的時(shí)候增加對象的橫縱向大小,以完全充滿其容器。水平方向填充。
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。附加選項(xiàng),用于按照容器的邊來剪切對象的頂部和/或底部的內(nèi)容。 剪切基于其縱向?qū)R設(shè)置:頂部對齊時(shí),剪切底部;底部對齊時(shí)剪切頂部;除此之外剪切頂部和底部。垂直方向裁剪。
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。附加選項(xiàng),用于按照容器的邊來剪切對象的左側(cè)和/或右側(cè)的內(nèi)容。 剪切基于其橫向?qū)R設(shè)置:左側(cè)對齊時(shí),剪切右側(cè);右側(cè)對齊時(shí)剪切左側(cè);除此之外剪切左側(cè)和右側(cè)。水平方向裁剪。
?
3、xml代碼實(shí)現(xiàn)
界面的橫向設(shè)置與背景圖的添加很容易實(shí)現(xiàn),針對按鈕靠右居中,剛開始沒有想到利用orientation這個(gè)屬性,想當(dāng)然地以為layout_gravity能夠搞定,所以過程有點(diǎn)波折。
整體設(shè)計(jì)思路為:最外層放置一個(gè)LinearLayout,設(shè)置子組件的布局為垂直方向居中并靠右,代碼如下:
1 android:layout_width="match_parent" 2 android:layout_height="match_parent" 3 android:gravity="center_vertical|right" 4 android:background="@drawable/login_bg"里層再放置一個(gè)LinearLayout,用來放置具體的按鈕。通過上一步的設(shè)置,該LinearLayout顯示在了界面的右邊,并且是垂直方向居中(其實(shí)一般應(yīng)用中是讓該LinearLayout的layout_height屬性為match_parent,故父組件中g(shù)ravity屬性的center_vertical值可以不添加)。
到這里,完成了大半,接下來就是添加按鈕。注意,目標(biāo)是讓按鈕在界面上靠右并居中。剛開始將內(nèi)層LinearLayout的gravity屬性設(shè)置為垂直居中,縱向鋪滿屏幕,為了美觀,讓其距界面右邊界40個(gè)像素?zé)o關(guān)點(diǎn)。代碼如下:
1 android:layout_width="wrap_content" 2 android:layout_height="match_parent" 3 android:gravity="center_vertical" 4 android:layout_marginRight="@dimen/margin_Right"但是問題來了,添加三個(gè)按鈕,以為能夠達(dá)到預(yù)想的效果:水平與垂直方向上均居中地排布在內(nèi)層的LinearLayout中,并且第二個(gè)在第一個(gè)下面,第三個(gè)在第二個(gè)下面。然而,現(xiàn)實(shí)是這樣的:
后面看了一些例子,恍然大悟,子組件的縱向或橫向上的排布,需要借助orientation屬性。馬上加入代碼:
android:orientation="vertical"
滿意的結(jié)果出現(xiàn)了:
三個(gè)按鈕的實(shí)現(xiàn)代碼如下:
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" />代碼中對按鈕的大小、具體位置、文本(顏色、大小)進(jìn)行了設(shè)置。
可以發(fā)現(xiàn),若僅將LinearLayout的gravity屬性設(shè)置為垂直居中,那么當(dāng)子組件個(gè)數(shù)大于1時(shí),會全部排列在居中位置,放不下只會在水平方向上錯(cuò)位顯示,并不會從上到下依次排布,此時(shí)就需要借助orientation屬性了。
轉(zhuǎn)載于:https://www.cnblogs.com/tgyf/p/4726413.html
總結(jié)
以上是生活随笔為你收集整理的gravity、layout_gravity及orientation的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Xamarin开发 Android 系
- 下一篇: junit基础学习之-断言注解(3)