将可绘制对象置于带背景且无文本的Button中 [英] Center a drawable in a Button with background and no text
问题描述
我的Android应用中有几个按钮。它们每个看起来都像这样:
I have a few buttons in my Android app. They each look kinda like this:
<Button
android:id="@+id/button_1"
android:drawableTop="@drawable/an_icon"
android:background="@drawable/a_background"
android:layout_width="size_dp"
android:layout_height="size_dp">
</Button>
当然,我是Android开发人员的新手,但我不知道如何居中在我的按钮中可绘制。我尝试了按钮及其容器以及drawablePadding的不同drawableLocations,边距和填充,但似乎无法完成。有没有一种方法可以使可绘制对象居中而不放弃另一个视图的按钮?
Granted, I'm new to Android dev, but I can't figure out how to center the drawable in my button. I've tried the different drawableLocations, margins and padding for the button and its container, as well as drawablePadding, but I can't seem to get it done. Is there a way to center the drawable without abandoning Button for another View?
推荐答案
居中
drawable
而不放弃Button
的方法
因此您可以通过使用 ImageView
来同时拥有(背景和可绘制对象)(如下所示):(因此无需使用Button)
So you can have both (background & drawable) by using ImageView
as below: (So No need to use Button)
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/an_icon"
android:background="@drawable/a_background" />
对于 OnClick
> ImageView :
For OnClick
of ImageView
:
findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
这篇关于将可绘制对象置于带背景且无文本的Button中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!