Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在使用setCompoundDrawableSwithinInstincBounds()时设置图像大小?_Android_Bitmap_Android Drawable - Fatal编程技术网

Android 如何在使用setCompoundDrawableSwithinInstincBounds()时设置图像大小?

Android 如何在使用setCompoundDrawableSwithinInstincBounds()时设置图像大小?,android,bitmap,android-drawable,Android,Bitmap,Android Drawable,我需要在选项卡布局的选项卡中设置文本上方的图像。所以我在TextView中使用setCompoundDrawablesWithIntrinsicBounds设置图像,但我不知道如何给出图像的大小。 我试着给出这样的尺寸: Drawable dr = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon); Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();

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

我试着给出这样的尺寸:

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);
但它也不起作用吗


那么,在使用SetCompoundDrawableSwithinInsitCBounds时,如何给出图像大小呢?

请查看此信息

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

公认的答案已经过时,而且有问题。。。 无法使用
setCompoundDrawableSwithinInstincBounds

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

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

在对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);
text=parentView.findviewbyd(Resource.Id.text);
Drawable img=ContextCompat.getDrawable(context,R.id.resource\u id);
img.倒退(0,0,20,20);
SetCompoundDrawables(img,null,null,null);

getDrawable
已被弃用……。我们可以使用`img=ContextCompat.getDrawable(MainActivity.this,R.drawable.mobile_图标)`代替
res.getDrawable
。最后一行的img_关闭了什么?@AnoopM请删除
Resources res=getResources()行并将
img\u off
替换为
img
可能的副本仅供澄清-我查看了编辑历史记录。自2015年11月5日7时27分以来,已接受答案中使用了
setCompoundDrawables
。谢谢