gpt4 book ai didi

android - 带有图像和文本的按钮,放置图像的方式

转载 作者:行者123 更新时间:2023-11-30 04:41:45 28 4
gpt4 key购买 nike

在我的应用程序中,我有几个带有小图像的按钮。我用

将它们添加到按钮中
android:drawableLeft="picture"

现在,如果我的按钮是 fill_parent,那么图片当然会显示在左侧,而我的文字会居中。现在我的问题是,有什么方法可以让这些图片居中或将它们放在文本旁边吗?

最佳答案

你可以使用它,因为它对我有帮助

@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);
}

关于android - 带有图像和文本的按钮,放置图像的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5867936/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com