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

时间:2013-04-04 08:50:40

标签: 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

有什么想法吗?

1 个答案:

答案 0 :(得分: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;
相关问题