LinearLayout-layout_gravity 屬性沒有效果分析


今天在一個布局文件中,遇到了一個問題,先看代碼

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="21dip"
        android:paddingLeft="@dimen/setup_fragment_padding_left"
        android:paddingRight="@dimen/setup_fragment_padding_right" >

        <!-- Buttons below -->
            <!--
        In order to show these buttons above the IME keyboard, we need to special case the to
        padding to a smaller height.
            -->
        <Button
            android:id="@+id/manual_setup"
            style="@style/accountSetupButtonVfive"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/button_margin_left"
            android:background="@drawable/email_btn_set"
            android:text="@string/account_setup_basics_manual_setup_action" />

        <Button
            android:id="@+id/next"
            style="@style/accountSetupButtonVfive"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:background="@drawable/email_btn_next"
            android:layout_marginRight="@dimen/button_margin_right"
            android:text="@string/next_action" />
    </LinearLayout>

上述代碼的目的,就是讓兩個按鈕,一個靠左邊,一個靠右邊。我增加了一個屬性

<android:layout_gravity="right">

結果發現一直不起作用。后來在網上查到了相關的解釋

如下

layout_gravity 表示組件自身在父組件中的位置
gravity 表示組件的子組件在組件中的位置

看似很簡單嘛

為什么這么簡單的道理,總有同學會發現,在“某些時候”,layout_gravity這個屬性不好使了,失去了它應有的作用

問題究竟出在哪里了呢?

當作為父layout的LinearLayout的屬性為android:orientation="vertical" 的時候,android:layout_gravity="?"這里設為橫向的時候才能生效。比如:left,right,center_horizontal等;
當作為父layout的LinearLayout的屬性為android:orientation="horizental" 的時候,android:layout_gravity="?"這里設為縱向的時候才能生效。比如:top,bottom,center_vertical等;
有一個比較特殊的是center,不管是橫向還是縱向的時候,它總有一個方向起作用, 因為LinearLayout他只可能有一個方向,

這nm的,確實讓人蛋疼。其實也有點道理吧,就是LinearLayout橫向的時候,如果有多個孩子,那就不知道把誰放最右了,

有兩個解決方法吧,

(1)用RelativeLayout吧,這個算是費話吧 ,哈哈

(2)在LinearLayout中設置android:gravity這個從官方api的解釋是怎么放置它的內容,LinearLayout的內容不就是他的孩子么,問題解決

現在根據它的提示,進行驗證

1)在LinearLayout 中添加gravity屬性

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
     android:background="#ff0000"
android:orientation="horizontal" android:paddingBottom="21dip" android:gravity="bottom" android:paddingLeft="@dimen/setup_fragment_padding_left" android:paddingRight="@dimen/setup_fragment_padding_right" > <Button android:id="@+id/manual_setup" style="@style/accountSetupButtonVfive" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/button_margin_left" android:background="@drawable/email_btn_set" android:text="@string/account_setup_basics_manual_setup_action" /> <Button android:id="@+id/next" style="@style/accountSetupButtonVfive" android:layout_width="120dp" android:layout_height="wrap_content" android:background="@drawable/email_btn_next" android:layout_marginRight="@dimen/button_margin_right" android:text="@string/next_action" /> </LinearLayout>

結果:

所以,gravity是有效的

2)在單個button中添加layout_gravity屬性

代碼

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#ff0000"
        android:orientation="horizontal"
        android:paddingBottom="21dip"
        android:paddingLeft="@dimen/setup_fragment_padding_left"
        android:paddingRight="@dimen/setup_fragment_padding_right" >

        <!-- Buttons below -->
            <!--
        In order to show these buttons above the IME keyboard, we need to special case the to
        padding to a smaller height.
            -->
        <Button
            android:id="@+id/manual_setup"
            style="@style/accountSetupButtonVfive"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/button_margin_left"
            android:background="@drawable/email_btn_set"
            android:layout_gravity="bottom"
            android:text="@string/account_setup_basics_manual_setup_action" />

        <Button
            android:id="@+id/next"
            style="@style/accountSetupButtonVfive"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:background="@drawable/email_btn_next"
            android:layout_marginRight="@dimen/button_margin_right"
            android:text="@string/next_action" />
    </LinearLayout>

結果如下

有效果的

3)如果添加的是layout_gravity="right"屬性,已經驗證沒效果。

這就證明了上述結論

當作為父layout的LinearLayout的屬性為android:orientation="vertical" 的時候,android:layout_gravity="?"這里設為橫向的時候才能生效。比如:left,right,center_horizontal等;
當作為父layout的LinearLayout的屬性為android:orientation="horizental" 的時候,android:layout_gravity="?"這里設為縱向的時候才能生效。比如:top,bottom,center_vertical

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM