Расположение Android Layout слева и справа в горизонтальной компоновке

У меня есть ImageView и ImageButton. Я имею их рядом друг с другом в горизонтальной компоновке. Я пытаюсь сделать так, чтобы изображение было выровнено на экране, а кнопка была выровнена вправо. Я попытался установить гравитацию, но, похоже, это не имеет значения. Где я ошибаюсь?

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:gravity="left"
        android:src="@drawable/short_logo" />

    <ImageButton
        android:id="@+id/terminateSeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:background="@null"
        android:src="@drawable/unregister_icon" />
</LinearLayout>

Ответы

Ответ 1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

Ответ 2

Использование...

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1.0" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_marginRight="80dp"
    android:layout_weight="0.5"
    android:gravity="left"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="right"
    android:layout_marginLeft="80dp"
    android:background="@null"
    android:src="@drawable/ic_launcher" />
</LinearLayout>

Ответ 3

Используйте этот пример, если вы хотите использовать LinearLayout без указания веса

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
       />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="right"
        />
    </LinearLayout>

Ответ 4

<RelativeLayout 
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"

    android:layout_alignParentLeft="true"
    android:src="@drawable/short_logo" />

<ImageButton
    android:id="@+id/terminateSeed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@null"
    android:src="@drawable/unregister_icon" />
</RelativeLayout >

Ответ 5

Сделайте imageview и imagebutton wrap_content и установите layout_gravity соответственно влево и вправо.

Ответ 6

a RelativeLayout решит вашу проблему, но если вы все еще настаиваете на использовании LinearLayout (просто потому, что мы задиры и делаем что-то трудное), вы делаете это так:

Ваш родительский LinearLayout является заполнителем-родителем, выравниванием по левому краю и горизонтальной ориентацией, и содержит ваш ImageView, а другой LinearLayout.

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"

Второй LinearLayout является заполняющим, выравниваемым по правому краю и горизонтально ориентированным и содержит ваш ImageButton.

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"