使用setCompoundDrawablesWithIntrinsicBounds()时如何设置图像大小? [英] How to set image size when using setCompoundDrawablesWithIntrinsicBounds()?

查看:430
本文介绍了使用setCompoundDrawablesWithIntrinsicBounds()时如何设置图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在标签布局"的标签中的文字上方设置图片.因此,我使用setCompoundDrawablesWithIntrinsicBounds在TextView中设置图像,但是我不知道如何为图像设置尺寸.

我试图给出这样的尺寸:

I need to set image above text in tabs in Tab Layout. So I set image in my TextView using setCompoundDrawablesWithIntrinsicBounds but I don't know how give size for my image.

I tried to give size like this:

Drawable dr = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    mobile_drawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));

    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("Mobile");
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0,mobile_drawable,0,0);
    tabLayout.getTabAt(0).setCustomView(tabOne);

但是它给了我这个错误:

But it gives me this error:

Cannot resolve method setCompoundDrawablesWithIntrinsicBounds(int,android.graphics.drawable.Drawable,int,int);

我也尝试过

tabOne.setCompoundDrawables(0,mobile_drawable,0,0);

但是它也不起作用吗?

那么当使用setCompoundDrawablesWithIntrinsicBounds时如何给图像尺寸呢?

So how to give image size when using setCompoundDrawablesWithIntrinsicBounds???

推荐答案

查看

Drawable img = ContextCompat.getDrawable(MainActivity.this,R.drawable.btn_img);
// You need to setBounds before setCompoundDrawables , or it couldn't display
img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
btn.setCompoundDrawables(img, null, null, null); 

将setCompoundDrawables用于EditText时计算图像大小

这篇关于使用setCompoundDrawablesWithIntrinsicBounds()时如何设置图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆