【发布时间】:2014-03-03 08:04:43
【问题描述】:
我已经查看了关于此的各种问题,但无法让它们发挥作用。
我有一个按钮,有背景,顶部是图像,底部是文本。该按钮是动态的,因此没有可用的 XML。
动态按钮的代码:
final Button image = new Button(this);
image.setTag(i);
image.setText(buttonsList[i].toUpperCase());
image.setGravity(Gravity.CENTER);
image.setBackgroundColor(getResources().getColor(R.color.dark_grey));
if (i == Const.MainIndex.FESTIVAL_OUTLOOK_INDEX) {
try {
XmlResourceParser parser = getResources().getXml(
R.color.text_color_green);
ColorStateList colors = ColorStateList.createFromXml(
getResources(), parser);
image.setTextColor(colors);
} catch (Exception e) {
}
//setting this value to -ve decreases the
//gap between the image and text
image.setCompoundDrawablePadding(-100);
image.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(
R.drawable.sidemenu_image_festout), null, null);
image.setBackgroundResource(R.drawable.button_background_green);
}
这是图像现在的样子..
我想在图像的顶部设置填充。这样图像看起来就不会靠近按钮的边框。我怎样才能做到这一点?
我尝试设置image.setPadding(0,10,0,0),但没有效果。
【问题讨论】:
-
试试图片的边距
-
也许你应该转换成dp,这个问题可能对你有帮助:stackoverflow.com/questions/9685658
-
@Fllo,我试了一下。没有效果。
-
你弄明白了吗?我认为这不能通过创建按钮来完成。创建线性或相对的动态布局,并像按钮一样处理点击