2013-08-02 217 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