在Android Button中,将图标与文本左对齐

3
我有一个按钮,我想在按钮文本的左侧放置一张图片。
    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

任何想法?

那么问题是什么? - KunalK
请查看我提出的问题中放置在左侧但不仅仅局限于文本左侧的图像。 - Bae
图片已经在左边了,但您想把文字放在哪里? - Paresh Mayani
我想看看我在问题中放的最后一张照片(抱歉,我之前忘记放了)。 - Bae
1个回答

0

使用这个

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;

-1和LinearLayout.LayoutParams.MATCH_PARENT是一样的,而-2和LinearLayout.LayoutParams.WRAP_CONTENT是一样的! - Bae

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接