我有一个按钮,我想在按钮文本的左侧放置一个图像。
Button b = new Button(this);
l.addView(b);
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(-1, -2);
b.setLayoutParams(pa);
b.setText("Prueba");
Drawable img = getResources().getDrawable(R.drawable.ic_launcher);
b.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
但是按钮宽度与父按钮匹配,图像在按钮左侧对齐,文本在按钮中心对齐。
编辑:
我看到了
..但我希望图像仅对齐左侧文本,而不是仅对齐按钮左边缘的右侧
我想看到这样的:
http://img543.imageshack.us/img543/6534/img20130404111534.jpg
有什么想法吗?
最佳答案
使用这个
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
如果要设置重力 用这个
LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
pa.gravity=Gravity.RIGHT;
关于android - 将图标与 Button android 中的文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15806541/