android - 将图标与 Button android 中的文本对齐

标签 android image button android-drawable

我有一个按钮,我想在按钮文本的左侧放置一个图像。

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

但是按钮宽度与父按钮匹配,图像在按钮左侧对齐,文本在按钮中心对齐。

编辑:

我看到了

enter image description here

..但我希望图像仅对齐左侧文本,而不是仅对齐按钮左边缘的右侧

我想看到这样的:

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/

相关文章:

java - 用按钮刷新java程序

android - 如何在设备上引起 onLowMemory 进行测试

Android:Button 的 onClick 方法不是线性处理的?

android - 即使在 Android 溢出期间也使文本底部对齐

asp.net - 如何创建 404 图像?对于 ASP.NET 无法读取的图像

Jquery Ajax 大拇指画廊

c++ - 如何生成位图头数据然后使用C++写入文件

jquery - "Select all text in iframe"按钮?

android - 如何在其父级的中心对齐 LinearLayout?

Android 报告 "Error=Unable to find instrumentation info for: ComponentInfo {}"