【问题标题】:How to set Top padding for image of a dynamic button with image and text如何为带有图像和文本的动态按钮的图像设置顶部填充
【发布时间】:2014-03-03 08:04:43
【问题描述】:

我已经查看了关于此的各种问题,但无法让它们发挥作用。

我有一个按钮,有背景,顶部是图像,底部是文本。该按钮是动态的,因此没有可用的 XML。

动态按钮的代码:

            final Button image = new Button(this);
            image.setTag(i);
            image.setText(buttonsList[i].toUpperCase());
            image.setGravity(Gravity.CENTER);
            image.setBackgroundColor(getResources().getColor(R.color.dark_grey));

            if (i == Const.MainIndex.FESTIVAL_OUTLOOK_INDEX) {

                try {
                    XmlResourceParser parser = getResources().getXml(
                            R.color.text_color_green);
                    ColorStateList colors = ColorStateList.createFromXml(
                            getResources(), parser);
                    image.setTextColor(colors);
                } catch (Exception e) {

                }

                //setting this value to -ve decreases the 
                            //gap between the image and text
                image.setCompoundDrawablePadding(-100);
                image.setCompoundDrawablesWithIntrinsicBounds(
                        null,
                        getResources().getDrawable(
                                R.drawable.sidemenu_image_festout), null, null);




                image.setBackgroundResource(R.drawable.button_background_green);
            }

这是图像现在的样子..

我想在图像的顶部设置填充。这样图像看起来就不会靠近按钮的边框。我怎样才能做到这一点?

我尝试设置image.setPadding(0,10,0,0),但没有效果。

【问题讨论】:

  • 试试图片的边距
  • 也许你应该转换成dp,这个问题可能对你有帮助:stackoverflow.com/questions/9685658
  • @Fllo,我试了一下。没有效果。
  • 你弄明白了吗?我认为这不能通过创建按钮来完成。创建线性或相对的动态布局,并像按钮一样处理点击

标签: android android-button


【解决方案1】:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

使用这个

【讨论】:

  • 它是一个动态按钮,如何获取 LayoutParams?
猜你喜欢
  • 2021-12-23
  • 2017-11-13
  • 2012-05-31
  • 1970-01-01
  • 2015-02-07
  • 2019-06-13
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多