android - 使用 setCompoundDrawablesWithIntrinsicBounds() 时如何设置图像大小?

标签 android bitmap android-drawable

我需要在选项卡布局中的选项卡中将图像设置在文本上方。所以我使用 setCompoundDrawablesWithIntrinsicBounds 在我的 TextView 中设置图像,但我不知道如何为我的图像指定大小。

我试着给出这样的尺寸:

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);

但它给了我这个错误:

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

我也试过

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

但它也不起作用?

那么在使用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); 

Calculate image size when using setCompoundDrawables for EditText

关于android - 使用 setCompoundDrawablesWithIntrinsicBounds() 时如何设置图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538658/

相关文章:

android - SharedPreferences 的范围

android - Android 中不显示选项菜单

Android-如何将图像保存在选择的文件夹中

Android - 两个 onClick 监听器和一个按钮

ios - 性能 UIImageView 与 UIView 与 QuartzCore

ios - CGContext:位图背景颜色:无法设置为白色:IOS

Android画一个带边框的半圆

java - 图像不是使用 BitmapFactory.decodeByteArray 创建的

android - 如何在EditText中添加两个drawableRight?

Android 可绘制图像不显示