【问题标题】:Align icon just left to the text in Button android将图标左对齐到Button android中的文本
【发布时间】:2013-03-26 06:02:47
【问题描述】:

我有一个按钮,我想在按钮文本的左边放一张图片。

    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

有什么想法吗?

【问题讨论】:

  • 那么问题是什么?
  • 查看我在问题中提出的图像。图像与文本的左侧对齐,但不仅仅是文本的左侧..
  • 图像已经在左侧,但您希望文本在哪里?
  • 我想看看我在问题中放的最后一张照片(对不起,我之前忘记放了)

标签: android image button android-drawable


【解决方案1】:

使用这个

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 相同!
猜你喜欢
  • 2015-04-24
  • 2020-05-13
  • 2019-10-01
  • 2012-10-13
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2017-10-20
  • 2015-01-25
相关资源
最近更新 更多