2

私はRelativeLayoutwith one ImageButton、 one ToggleButton、およびその他のコントロールを持っています。RelativeLayout の右側に配置された両方のボタン。同じ高さにしてほしい。ここに私のレイアウトxmlがあります:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingBottom="10dip" >

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/copy"
        android:layout_alignParentRight="true" />

    <ToggleButton
        android:id="@+id/buttonView"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:textOn="Hide"
        android:textOff="View" 
        android:layout_toLeftOf="@id/imageButton" 
        android:layout_alignTop="@id/imageButton"
        android:layout_alignBottom="@id/imageButton" />

    <!-- ... other controls ... -->
</RelativeLayout>

ToggleButton を ImageButton と同じ高さに設定layout_height="0dip"して使用しようとしましlayout_alignTopたが、これは機能しません。layout_alignBottom

ここに画像の説明を入力

ご覧のとおり、ToggleButton は正しく配置されておらず、常に ImageButton よりわずかに高くなっています。私は何を間違っていますか?

Android 2.3 でのテスト

4

3 に答える 3

2

このようにしてみてください。

         <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip" >

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />

    <ToggleButton
        android:id="@+id/buttonView"
        android:layout_width="55dp"
        android:layout_height="54dp"
        android:layout_alignTop="@id/imageButton"
        android:layout_toLeftOf="@+id/imageButton"
        android:textOff="View"
        android:textOn="Hide" />

    <!-- ... other controls ... -->
</RelativeLayout>

トグルボタンは、画像ボタンと比較して少し高さが高くなります。画像ボタンの幅と高さを完全なものよりも大きくする必要があります。同じ幅と高さを使用して、わずかな違いを見つけることができます。

ここに画像の説明を入力してください

于 2013-01-18T09:02:59.533 に答える
1

への変更

<ImageButton
    android:id="@+id/imageButton"
    android:layout_height="150dp"//some height
    ...

<ToggleButton
    android:id="@+id/buttonView"
    android:layout_height="150dp"//same height as above
于 2013-01-18T08:58:44.897 に答える
1
  1. LinearLayoutの子として水平を持ち、それにand RelativeLayoutを追加します。ToggleButtonImageView
  2. ToggleButtonセット用android:layout_height="match_parent"

編集:
これが作業コードです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingBottom="10dip" >

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

        <ToggleButton
            android:id="@+id/buttonView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textOff="View"
            android:textOn="Hide" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/copy" />
    </LinearLayout>
</RelativeLayout>

編集2:

あなたの問題を理解しました。上記のコード、デフォルトの Holo テーマを使用する Android 4.0 でのみ機能するようです。

Android 2.3 では、デフォルトのテーマで、 のスタイルはImageButton背景を として指定します。この場合btn_default_normal、ピクセルの一番上の行は透明です。同じテーマのToggleButtonの背景ではbtn_default_small_normal、一番上の行に不透明なピクセルがある が使用されています。(リンクをクリックして、ご自分で確認してください。)

したがって、 と並べて配置すると、同じ高さToggleButtonではないように見えます。

于 2013-01-18T09:05:48.320 に答える