【问题标题】:DrawableTop not showing drawableDrawableTop 不显示drawable
【发布时间】:2012-07-02 10:27:44
【问题描述】:

我正在尝试使用 drawableTop 在按钮中的文本上方显示图片。

我的图片来自 sdcard,不是可绘制的。

此外,按钮的大小可能会根据显示的按钮数量而变化。更多按钮,更小的按钮,然后是更小的图像。

这是我创建按钮的方法:

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

这是 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>

这是我创建drawableTop的方法:

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

文本显示在按钮中,但图像没有出现。

哪里出错了?

感谢任何帮助!

【问题讨论】:

    标签: android drawable compound-drawables


    【解决方案1】:

    我想缺少的是让您的可绘制对象已经调用了setBounds(Rect) 还可以尝试将图片加载到位图中,然后将其放入可绘制对象中:

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

    使用 SetBounds :

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

    例如使用 50x50 尺寸:

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

    【讨论】:

    • 你好莫娜!谢谢你的回复,对不起...我是菜鸟,我该如何设置Bounds?
    • 你好Mona,谢谢你的回答。无论如何,图像的大小会根据显示的按钮数量而变化。更多按钮,更小的图像。当我使用 ImageButton 时,我不需要担心设置大小。有没有办法使用“重量”之类的方法来使用您的解决方案?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多