【问题标题】:How to set image size when using setCompoundDrawablesWithIntrinsicBounds()?使用 setCompoundDrawablesWithIntrinsicBounds() 时如何设置图像大小?
【发布时间】:2015-11-05 07:17:03
【问题描述】:

我需要在选项卡布局中的选项卡中将图像设置在文本上方。所以我使用 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时如何给图片尺寸???

【问题讨论】:

标签: android bitmap android-drawable


【解决方案1】:

看看这个

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

【讨论】:

  • getDrawable 已弃用........我们可以使用`img = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon);`代替res.getDrawable
  • 最后一行的 img_off 是什么?
  • @AnoopM 请删除Resources res = getResources(); 行并将img_off 替换为img
【解决方案2】:

接受的答案已过时且有问题...

您无法使用setCompoundDrawablesWithIntrinsicBounds 设置可绘制尺寸

相反,您需要像这样使用setCompoundDrawables 进行设置

Drawable img = ContextCompat.getDrawable(yourContext, R.drawable.yourImgId);
img.setBounds(0, 0, yourSize, yourSize);
yourTextView.setCompoundDrawables(img, null, null, null);

【讨论】:

  • 只是为了您的澄清 - 我浏览了编辑历史记录。由于Nov 5 '15 at 7:27 本身setCompoundDrawables 在接受的答案中使用。谢谢。
【解决方案3】:

在 VS.Xamarin 中什么对我有用

text = parentView.FindViewById<EditText>(Resource.Id.text);
Drawable img = ContextCompat.getDrawable(context, R.id.resource_id);
img.SetBounds(0, 0, 20, 20); 
text.SetCompoundDrawables(img, null, null, null);

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多