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
。谢谢