21

私は 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>
4

7 に答える 7

32
<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>
于 2012-04-27T17:36:56.213 に答える
12

使用する...

 <?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>
于 2012-04-27T17:40:46.300 に答える
3

Weight を指定せずに 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>
于 2015-04-04T07:08:50.317 に答える
0

それimageviewに応じて作成し、左右にimagebutton wrap_content設定します。layout_gravity

于 2012-04-27T17:41:35.573 に答える
0

aRelativeLayoutスナップで問題を解決しますが、それでも LinearLayout を使用することに固執する場合 (私たちが悪いことをしているという理由だけで)、次のようにします。

親の LinearLayout は、fill-parent、左揃え、水平方向であり、ImageView と別の LinearLayout を含みます。

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

2 番目の LinearLayout は、fill-parent、右揃え、水平方向であり、ImageButton を含みます。

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
于 2012-04-27T17:47:18.703 に答える
0
<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 >
于 2012-04-27T17:39:34.083 に答える