1

我有 LinearLayout (视频按钮容器)和图像按钮,因为它是孩子。我希望那个视频按钮右对齐,所以我给了它layout_gravity="right"

<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>

它产生这个:

在此处输入图像描述

我想要的是这样的:

在此处输入图像描述

我通过将视频按钮容器更改android:orientation="horizontal"android:orientation="vertical". 发生了什么?为什么它不适用于容器的水平方向?

4

2 回答 2

8

如果容器是水平的,那么它应该从左到右按顺序堆叠元素。现在,如果是这样,它如何在保持其原始前提的同时满足水平布局重力?

水平重力(右、左、中)将在垂直 LinearLayout 中起作用,而垂直重力(顶部、底部)将在水平 LinearLayout 中起作用。

于 2013-08-02T22:54:45.600 回答
2

就放

重力=“对”

进入水平线性布局,它应该可以解决问题:)

在我的情况下效果很好(只是基于您的布局的一个例子):

<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 的方向:垂直和水平允许定义布局内的子项将以哪种方式彼此相邻放置,水平或垂直。

重力属性允许您控制布局的锚点,在您的情况下,它应该位于布局的右侧。

于 2013-08-02T22:53:05.830 回答