1

カスタムの背景、テキスト、およびテキストの右側に画像を備えた2つの同じサイズのラジオボタンを作成したかったのです。これらは標準の「ボタン」とは非常に異なるため、クリック可能な「RelativeLayout」を使用して作成しました。

テキストと画像の高さは異なりますが、それぞれをボタンの垂直方向の中央に配置したいと思います。また、テキストと画像の組み合わせをボタンの水平方向の中央に配置したいと思います。この2番目の部分は、私が問題を抱えている部分です。中心から外れていて、左側に近いです。下の画像では、左側が私が欲しいものですが、右側が起こっていることです。ボタンの1つ(テキストが長いもの)の画像も小さくなります...ボタンの右側にはまだ十分なスペースがありますが。

私が欲しいのは左側にあり、起こっていることは右側にあります。

これが私のコードです:

<LinearLayout
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RelativeLayout
        android:id="@+id/my_button"
        android:background="@drawable/radio_button"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <TextView
            android:id="@+id/button_textview"
            android:text="@string/button_label"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <ImageView
            android:contentDescription="@string/image_description"
            android:id="@+id/button_imageview"
            android:layout_centerVertical="true"
            android:src="@drawable/my_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/button_textview">
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        ... same thing for the second button ...
    </RelativeLayout>

</LinearLayout>

私は何が間違っているのですか?

4

2 に答える 2

1

これをボタンとして使用します。

<LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_gravity="center_horizontal"
              >
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:text="MyButton"/>
    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:src="@drawable/my_image"/>

</LinearLayout>

これで、他の親ビューに配置できます。上記のボタンにレイアウト属性を適用するには、それらの属性を<LinearLayout>上記のボタンの外側のタグに配置します。

別:

TextView次のような属性を使用して 、カスタム画像を側面 (左、右、上、下) に描画するように設定できます。android:drawableLeft="@drawable/my_image"

于 2012-08-30T19:53:00.987 に答える
0

ソリューションが追加されていることがわかりました

android:paddingRight="0dip"

不思議なことに、そもそもパディングを入れていなかったのに。

于 2012-09-04T13:56:58.633 に答える