Ada*_*orm 43
您可以在按钮上声明要分配图像的位置(我假设您已经在按钮上有图像):
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- I'm a Button -"
android:drawableTop="@drawable/icon"
/>
Run Code Online (Sandbox Code Playgroud)
你也可以用android:drawablePadding(int)在文本和图像之间设置填充
drawableTop属性可以更改为drawableRight,Left,Bottom等.祝你好运!
你也可以这样做

<LinearLayout
android:id="@+id/image_button_2"
style="@android:style/Widget.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:src="@drawable/ic_gear" />
<TextView
android:id="@+id/image_button_2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="Button 2" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在你的活动中
final View button2 = findViewById(R.id.image_button_2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Run Code Online (Sandbox Code Playgroud)
如果我是你,我就不会使用按钮。使用LinearLayout或RelativeLayout并给它按钮背景(提示:如果你希望它为每个状态有不同的图像,请使用选择器)并将你的TextView放在其中,然后你可以使用drawableLeft,drawableTop等属性。将图片放在您想要的一侧。如果您想要对图片相对于文本的位置进行更高级别的控制,请使用一个 TextView 和一个 ImageView。然后,在您的 java 中,只需使用 setOnClickListener() 获取对布局的引用并像对待按钮一样对待它。