Android中將控件放到線性布局的任意位置(三)

orientationlayout_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)效果為:


    1.png
  • 可以發(fā)現(xiàn)麸拄,屬性layout_gravity確實能夠讓TextView按預想的“放置”,那么豎直方向不能讓控件靠著底部黔姜,莫不是有什么“沖突了”拢切?

  • 注意屬性android:orientation,它的意思嘛,是方向秆吵,在布局中聲明該屬性淮椰,則在該布局容器之中的子控件都會按照該屬性的值布局,這里需要了解一下:

  1. 在所有布局中纳寂,默認情況下主穗,它的子控件會從父控件的左上角開始布局(這個方向,和計算機領(lǐng)域烈疚,屏幕的方向是相同的黔牵,從左上角開始,向右為X正方向爷肝,向下為Y軸正方向猾浦,左上角坐標為(0,0),相應的灯抛,右下角為(maxWidth金赦,maxHeight)(兩者分別代表屏幕最大寬度、高度))
  2. 線性布局(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)為:
    2.png
  • 注意觀察通殃,留意到: 我們并沒有刪除屬性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"將失效

  • 上述猜想是我們實際操作得來轻专,那么在組合:

  1. 父布局:android:orientation="horizontal"忆矛,子控件:android:layout_gravity="bottom"
  2. 父布局: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>

效果:
3.png

代碼:

<?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>
  • 上述代碼效果:
    4.png
  • 可見催训,上面兩種組合是生效的。
  • 通過實際操作宗收,我們得出一個基本結(jié)論:
    1. 當父布局的屬性android:orientation="horizontal",其子控件屬性:android:layout_gravity=在水平方向?qū)⑹茫Q直方向不受影響,繼續(xù)起作用混稽。
    2. 當父布局的屬性android:orientation="vertical",其子控件屬性:android:layout_gravity=在豎直方向?qū)⑹Р勺ぃ椒较虿皇苡绊懀瑢⒗^續(xù)起作用匈勋。
  • 結(jié)論倒是有了:簡單來說礼旅,如果父布局規(guī)定了其子控件按照某一方向進行,那么子控件在該方向上調(diào)整自己位置的能力將消失洽洁。不過為什么會這樣呢痘系,我想有如下考慮:
  1. 兩個方向便能確定子控件在父控件的具體位置,父控件確定一個維度饿自,子控件本事確定一個維度汰翠,邏輯清晰明了。
  2. 最為關(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>
  • 該布局效果:
    5.png
  • 按照邏輯城豁,父布局設置了其子控件按照從上到下的序列排列苟穆,如果此時子控件還具有該方向上調(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)新的需求榔袋。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末周拐,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子凰兑,更是在濱河造成了極大的恐慌妥粟,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吏够,死亡現(xiàn)場離奇詭異勾给,居然都是意外死亡,警方通過查閱死者的電腦和手機锅知,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門播急,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人售睹,你說我怎么就攤上這事桩警。” “怎么了昌妹?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵捶枢,是天一觀的道長握截。 經(jīng)常有香客問我,道長烂叔,這世上最難降的妖魔是什么谨胞? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮长已,結(jié)果婚禮上畜眨,老公的妹妹穿的比我還像新娘。我一直安慰自己术瓮,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布贰健。 她就那樣靜靜地躺著胞四,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伶椿。 梳的紋絲不亂的頭發(fā)上辜伟,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天,我揣著相機與錄音脊另,去河邊找鬼导狡。 笑死,一個胖子當著我的面吹牛偎痛,可吹牛的內(nèi)容都是我干的旱捧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼踩麦,長吁一口氣:“原來是場噩夢啊……” “哼枚赡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谓谦,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤贫橙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后反粥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卢肃,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年才顿,在試婚紗的時候發(fā)現(xiàn)自己被綠了莫湘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡娜膘,死狀恐怖逊脯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情竣贪,我是刑警寧澤军洼,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布巩螃,位于F島的核電站,受9級特大地震影響匕争,放射性物質(zhì)發(fā)生泄漏避乏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一甘桑、第九天 我趴在偏房一處隱蔽的房頂上張望拍皮。 院中可真熱鬧,春花似錦跑杭、人聲如沸铆帽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爹橱。三九已至,卻和暖如春窄做,著一層夾襖步出監(jiān)牢的瞬間愧驱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工椭盏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留组砚,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓掏颊,卻偏偏與公主長得像糟红,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蚯舱,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容