2013-08-02 226 views
1

我有LinearLayout(視頻按鈕容器)和圖像按鈕,因爲它是孩子。我想讓那個視頻按鈕對齊,所以我給了它layout_gravity="right"Android線性佈局方向如何影響佈局引力?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <!-- VIDEO BUTTON CONTAINER --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="#000000" 
     android:layout_gravity="top"> 

     <!-- VIDEO BUTTON --> 
     <ImageButton 
      android:id="@+id/button_video" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:background="?android:attr/selectableItemBackground" 
      android:contentDescription="@string/desc" 
      android:paddingBottom="@dimen/controls_button_padding" 
      android:paddingTop="@dimen/controls_button_padding" 
      android:src="@drawable/ic_action_video" /> 
    </LinearLayout> 

    <!-- some FrameLayout and another LinearLayout --> 
</LinearLayout> 

它產生這樣的:

enter image description here

我想是這樣的:

enter image description here

我也得到通過改變視頻按鈕容器的android:orientation="horizontal"android:orientation="vertical"。 發生了什麼?爲什麼它不適用於容器的水平方向?

回答

7

如果容器是水平的,那麼它應該按順序從左到右堆疊元素。現在,如果是這樣的話,它如何在保持其原有前提的同時水平地滿足佈局重力?

水平重力(右,左,中)將在垂直LinearLayout中工作,而垂直重力(頂部,底部)將在水平LinearLayout中工作。

+0

嗨! orientation屬性做了什麼?當我從「垂直」狀態轉換爲「水平狀態」時,我的佈局沒有發現任何不同...謝謝! –

1

只要把

比重= 「右」

到水平線性佈局,它應該做的伎倆:)

作品以及我的情況(只是基於個例在您的佈局):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       tools:context=".MainActivity" > 

    <!-- VIDEO BUTTON CONTAINER --> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:background="#000000" 
      android:gravity="right"> 

     <!-- VIDEO BUTTON --> 
     <ImageButton 
       android:id="@+id/button_video" 
       android:layout_width="30dp" 
       android:layout_height="30dp" 
       android:layout_margin="10dp" 
       android:background="@android:color/background_light"/> 
    </LinearLayout> 

    <!-- some FrameLayout and another LinearLayout --> 
</LinearLayout> 

- 編輯 -

關於LinearLayout的方向:垂直和水平允許定義佈局內的子項將水平或垂直放置在彼此相鄰的位置。

重力屬性允許您控制佈局的錨點,在您的情況應該在佈局的右側。

+0

調整'View'中的內容...而不是其父代中的'View' – codeMagic