この機能に関する多くの質問を見て、その答えをたどろうとした後、もっと明確な例があるのではないかと思いました。
編集:「真ん中」にある画像とテキストを持つ大きなボタンを作成しようとしていました。ボタン (StateList ドローアブル) として動作する必要があり、画像/テキストのペアをグループ化して中央に配置する必要があります (グループとして)。
画像とテキストを含むボタンが必要な場合は、CompoundDrawableを使用してみませんか?
例えば:
また、確認してください:ImageViewとTextViewを含むLinearLayoutの代わりに複合ドローアブルを使用するにはどうすればよいですか
他の人の時間を節約するために、私はこれを提供します:
レイアウト/some_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/menu_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- StateList Drawable to make it look like a button -->
android:background="@drawable/btn_std_holo_states"
<!-- Required so you can click on it like a button -->
android:clickable="true"
<!-- Recommended min height from the guidelines -->
android:minHeight="48dp"
<!-- OnClickEvent definition -->
android:onClick="onClickOk" >
<!-- Compound drawable of graphic and text -->
<TextView
android:id="@+id/txt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- Center both the graphic and text inside the button -->
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<!-- Draw the graphic to the left of the text -->
android:drawableLeft="@drawable/ic_ok"
<!-- Space between the graphic and the text-->
android:drawablePadding="16dp"
<!-- ensures the text and graphic are both centered vertically -->
android:gravity="center"
<!-- Text of the button -->
android:text="@android:string/ok"
<!-- Change the font to match the standard button settings (optional) -->
android:textAppearance="?android:attr/textAppearanceButton" />
</RelativeLayout>
drawable/btn_std_holo_states.xml (上記参照)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/abs__btn_cab_done_pressed_holo_dark" android:state_pressed="true"/>
<item android:drawable="@drawable/abs__btn_cab_done_focused_holo_dark" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@android:color/transparent" android:state_enabled="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
注: ここでのさまざまな @drawable および @android:color 設定は何でもかまいません。完全な例を作成するためにのみ提供されています。
これを試して:
Drawable appImg = getApplicationContext().getResources().getDrawable( R.drawable.ic_launcher );
appImg.setBounds( 0, 0, appImg.getIntrinsicHeight(), appImg.getIntrinsicWidth() );
Button btn_ok = (Button) findViewById(R.id.ok);
btn_ok.setCompoundDrawables( null, null, appImg, null );
お役に立てば幸いです。
ありがとう。