Android LinearLayout将内容对齐到不同的边

时间:2016-02-11 23:24:00

标签: android layout

我仍然是不同Android布局的初学者。这是简单的线性布局:

<LinearLayout
        android:layout_height="50dp"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="end"
        android:padding="5dp">

        <ImageButton
            android:id="@+id/bt_template"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_template_button"
            android:scaleType="fitCenter"
            android:background="#ffffffff"
            android:contentDescription="Choose template"/>

        <ImageButton
            android:id="@+id/bt_captureImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_camera_button"
            android:scaleType="fitCenter"
            android:background="#ffffffff"
            android:contentDescription="Choose template"/>

        <Button
            style="@style/MyRedButton"
            android:layout_margin="0dp"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:text="@string/str_new_mail_activity_button_send"
            android:id="@+id/bt_send" />

    </LinearLayout>

我想要两个按钮向左移动,第三个按钮向右移动。我该怎么做?

enter image description here

蓝色按钮位于左侧,红色按钮位于右侧。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

如果你必须使用LinearLayout,给红色按钮赋予1的重量(因此它会占用空间)并确保它具有向右的重力,如果这是你需要的。

android:layout_weight="1"
android:layout_gravity="right"

答案 1 :(得分:0)

这是使用LinearLayout的可能选项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_height="50dp"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageButton
        android:background="#ffffffff"
        android:contentDescription="Choose template"
        android:id="@+id/bt_template"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_template_button" />

    <ImageButton
        android:background="#ffffffff"
        android:contentDescription="Choose template"
        android:id="@+id/bt_captureImage"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_camera_button" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        style="@style/MyRedButton"
        android:id="@+id/bt_send"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:layout_width="wrap_content"
        android:text="@string/str_new_mail_activity_button_send" />

</LinearLayout>

答案 2 :(得分:0)

检查出来:

    |    A    |    B    |    C    |    D    |    E    |    F    |    G    |
----+---------+---------+---------+---------+---------+---------+---------+
  1 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  2 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  3 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  4 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  5 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  6 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  7 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  8 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  9 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
 10 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+