简体   繁体   English

DrawableTop不显示可绘制

[英]DrawableTop not showing drawable

I am trying to use drawableTop to show a picture above a text in a button. 我正在尝试使用drawableTop在按钮中的文本上方显示图片。

My picture is from sdcard, not a drawable. 我的图片来自sdcard,而不是可绘制的。

Also, the size of the button may change depending on how many buttons are show. 此外,按钮的大小可能会根据显示的按钮数量而变化。 More buttons, smaller buttons, then, smaller images. 更多的按钮,较小的按钮,然后较小的图像。

Here is how I create my buttons: 这是我创建按钮的方式:

btn = (Button) LayoutInflater.from(
                            getBaseContext()).inflate(
                            R.layout.buttonstyle, l1, false);

Here is the XML file buttonstyle: 这是XML文件的按钮样式:

<?xml version="1.0" encoding="UTF-8"?>
        <Button   xmlns:android="http://schemas.android.com/apk/res/android" 
            android:background="@drawable/roundcorners" 
            android:id="@+id/buttonTest" 
            android:scaleType="centerInside"
            android:cropToPadding="false"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="10dip"
            android:layout_height="fill_parent" 
            android:layout_width="wrap_content"
            android:text="Test text"
            android:textSize="40dip"
            android:textStyle="bold"
            android:textColor="#000000">
        </Button>

And here is how I create the drawableTop: 这是我创建drawableTop的方法:

Drawable drawableTop = Drawable.createFromPath(endImagemVoltar); 
btn.setCompoundDrawables(null, drawableTop, null, null);

The text is shown in the button, but the image doesn't appear. 文本显示在按钮中,但未显示图像。

Where is the error? 错误在哪里?

Any help is appreciated! 任何帮助表示赞赏!

I guess what's missing is to for your drawable to already have had setBounds(Rect) called also try loading the picture in a bitmap then getting it in a drawable: 我想缺少的是您的drawable已经调用了setBounds(Rect),还尝试将图片加载到位图中,然后将其放入drawable中:

Bitmap bitmapImage = BitmapFactory.decodeFile(endImagemVoltar);
Drawable bgrImage = new BitmapDrawable(bitmapImage);

To use SetBounds : 要使用SetBounds:

 public void setBounds (int left, int top, int right, int bottom)

for a 50x50 size for example use : 例如50x50尺寸,请使用:

drawable.setBounds(0, 0, 50, 50); drawable.setBounds(0,0,50,50);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM