orientation
和layout_gravity
之間魚和熊掌的關(guān)系增淹?
- 從前面介紹中我們知道现横,在之前介紹中漓拾,我們想用屬性
layout_gravity
把藍色TextView這個控件放到屏幕右下角,但是失敗了戒祠。不能向右放的原因骇两,我們在Android中將控件放到線性布局的任意位置(二)分析了,是因為姜盈,該TextView已經(jīng)在寬度方面充滿了屏幕低千,不再具有向右的空間了。 - 然而向下是有足夠空間的馏颂,那么是哪兒出了問題示血,導致我們不能實現(xiàn)預期目標呢,將布局代碼稍微做下修改救拉,將TextView的寬度修改為與其“內(nèi)容”一樣寬(這樣难审,TextView便有了向右靠的空間,注意對比其與之前的區(qū)別)亿絮,并用
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:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:textSize="25sp"
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="第一個TextView"
android:textColor="#ffffff"
android:layout_gravity="right"
/>
</LinearLayout>
-
上述代碼表現(xiàn)效果為:
可以發(fā)現(xiàn)麸拄,屬性
layout_gravity
確實能夠讓TextView按預想的“放置”,那么豎直方向不能讓控件靠著底部黔姜,莫不是有什么“沖突了”拢切?注意屬性
android:orientation
,它的意思嘛,是方向秆吵,在布局中聲明該屬性淮椰,則在該布局容器之中的子控件都會按照該屬性的值布局,這里需要了解一下:
- 在所有布局中纳寂,默認情況下主穗,它的子控件會從父控件的左上角開始布局(這個方向,和計算機領(lǐng)域烈疚,屏幕的方向是相同的黔牵,從左上角開始,向右為X正方向爷肝,向下為Y軸正方向猾浦,左上角坐標為(0,0),相應的灯抛,右下角為(maxWidth金赦,maxHeight)(兩者分別代表屏幕最大寬度、高度))
- 線性布局(LinearLayout)对嚼,顧名思義夹抗,它的子控件會按照線性方向(X或Y方向)布局,至于是向X(水平)方向還是(豎直)方向布局纵竖,則就是有
android:orientation
指定的漠烧,默認情況下,線性布局布局會按照水平方向布局靡砌。
- 為說明已脓,取消該屬性,并增加一個TextViw:
<?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:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
>
<TextView
android:textSize="25sp"
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:text="第一個TextView"
android:textColor="#ffffff"
android:layout_gravity="right"
/>
<TextView
android:textSize="25sp"
android:id="@+id/tv_tow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#555555"
android:text="第二個TextView"
android:textColor="#ffffff"
android:layout_gravity="right"
/>
</LinearLayout>
-
上述代碼表現(xiàn)為:
注意觀察通殃,留意到: 我們并沒有刪除屬性
android:layout_gravity="right"
的度液,按照修改之前的邏輯,第一個画舌、第二個TextView都應該靠右才對堕担,然而事實是他們都從左邊,開始排過來曲聂,這似乎就是問題所在霹购!我們之前說過,默認情況下朋腋,線性布局子控件是水平方向布局的厕鹃,也就是相當于在LinearLayout添加了
android:orientation="horizontal"
,那么也就是說:
1. 當父布局的屬性為android:orientation="horizontal"
時兢仰,子控件屬性android:layout_gravity="right"
將失效注意到之前我們LinearLayout的布局一直都是:
android:orientation="vertical"
,那個時候剂碴,該線性布局子控件TextView的屬性:android:layout_gravity="bottom"
是失效的,這樣我們有:
2. 當父布局的屬性為android:orientation="vertical"
時,子控件屬性android:layout_gravity="bottom"
將失效上述猜想是我們實際操作得來轻专,那么在組合:
- 父布局:
android:orientation="horizontal"
忆矛,子控件:android:layout_gravity="bottom"
- 父布局:
android:orientation="vertical"
,子控件:android:layout_gravity="right"
會生效嗎请垛,我們試試:
<?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:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#0000ff"
android:text="第一個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_tow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#555555"
android:text="第二個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
效果:代碼:
<?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:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#0000ff"
android:text="第一個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_tow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#555555"
android:text="第二個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
-
上述代碼效果:
- 可見催训,上面兩種組合是生效的。
- 通過實際操作宗收,我們得出一個基本結(jié)論:
1. 當父布局的屬性android:orientation="horizontal"
,其子控件屬性:android:layout_gravity=
在水平方向?qū)⑹茫Q直方向不受影響,繼續(xù)起作用混稽。
2. 當父布局的屬性android:orientation="vertical"
,其子控件屬性:android:layout_gravity=
在豎直方向?qū)⑹Р勺ぃ椒较虿皇苡绊懀瑢⒗^續(xù)起作用匈勋。 - 結(jié)論倒是有了:簡單來說礼旅,如果父布局規(guī)定了其子控件按照某一方向進行,那么子控件在該方向上調(diào)整自己位置的能力將消失洽洁。不過為什么會這樣呢痘系,我想有如下考慮:
- 兩個方向便能確定子控件在父控件的具體位置,父控件確定一個維度饿自,子控件本事確定一個維度汰翠,邏輯清晰明了。
- 最為關(guān)鍵是昭雌,如果父控件和子控件都能在同一維度起作用的話复唤,會有明顯的沖突,比如布局:
<?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:id="@+id/ll_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#0000ff"
android:text="第一個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_tow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#555555"
android:text="第二個TextView"
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
-
該布局效果:
- 按照邏輯城豁,父布局設置了其子控件按照從上到下的序列排列苟穆,如果此時子控件還具有該方向上調(diào)整自身位置的能力,一旦某個控件做出調(diào)整要求唱星,如“”第一個TextView“”想把自己排列到屏幕底部雳旅,那么其他控件如何處之?“第一個TextView”可是應該排在他們前面的啊间聊,那么他們只能往下排攒盈,排到屏幕外了?這就是問題所在哎榴,又比如“第二個控件”想把自己排到屏幕最上方型豁,也會有相應問題:與之相反的是僵蛛,此時無論“第一個TextView”還是“第二個TextView”在左右(水平)方向上調(diào)整位置,都不會對其他控件產(chǎn)生影響(只要你還是在我前面或者后面迎变,至于是左前方充尉,右前方,還是正前方衣形,都沒有與父布局規(guī)定相沖突驼侠,這就可以了)。所以谆吴,個人認為倒源,正是基于這樣顯而易見沖突的考慮,當子控件試圖在父布局已經(jīng)規(guī)定排列方式的方向上調(diào)整自己的位置時句狼,調(diào)整位置的屬性將會自動失效笋熬。
這樣,我們便對屬性android:orientation
和屬性android:layout_gravity
有了進一步認識腻菇。 - 那么就如最后的代碼胳螟,我們在父布局已經(jīng)是豎直方向的情況下,我們究竟能不能通過某種手段芜繁,將“第二個TextView”放到屏幕底部呢旺隙,之前我們通過在父布局設置
android:gravity
到達過目的,不過這次骏令,我們有了新的需求了:第一個TextView在屏幕頂部蔬捷,第二個在屏幕底部。下一篇我們講講如何實現(xiàn)新的需求榔袋。