在我的应用程序中,我有几个带有小图像的按钮。我用
将它们添加到按钮中android:drawableLeft="picture"
现在,如果我的按钮是fill_parent
,则图片显示在左侧,当然我的文字居中。现在我的问题是,有没有办法将这些图像居中或将它们放在文本旁边?
答案 0 :(得分:1)
你可以使用它,因为它可以帮助我
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Call here getWidth() and getHeight()
applyTextOffset(yourButton, yourButton.getWidth(),R.drawable.recents); // where R.drawable.recents is your image which you want to set as background
}
这种方法可以将图像设置在文本附近
public void applyTextOffset(Button button, int buttonWidth, int image) {
int textWidth = (int) button.getPaint().measureText(
button.getText().toString());
int padding = (buttonWidth / 2)
- ((textWidth / 2)
+ ((BitmapDrawable) this.getResources().getDrawable(image)).getBitmap().getWidth());
button.setPadding(padding, 0, 0, 0);
button.setCompoundDrawablePadding(-padding);
}
答案 1 :(得分:0)
也许这不是解决问题的最佳方法,但是你尝试使用布局而不是按钮。例如,LinearLayout以正确的顺序包含您想要的所有内容,然后只需添加一个单击侦听器,使其像按钮一样工作。希望这会有所帮助。