getdrawable()能用什么替换
时间: 2024-03-29 15:41:18 浏览: 13
如果您在Android开发中使用getdrawable()方法时遇到问题,可以考虑使用ContextCompat.getDrawable()方法来替换。ContextCompat.getDrawable()方法可以确保您的应用程序在所有Android版本上获得相同的行为,而不需要直接使用不同版本的API。
相关问题
layout能使用getDrawable().getIntrinsicWidth()
在layout中,getDrawable().getIntrinsicWidth()并不能直接使用。因为这个方法是定义在Drawable类中的,而在layout中,我们通常使用的是View类及其子类,它们并没有直接提供这个方法。如果你想要获取一个View中的Drawable对象的固有宽度,你可以通过以下方式之一实现:
1. 通过findViewById()方法获取到View对象,然后通过View对象的getBackground()、getCompoundDrawables()等方法获取到对应的Drawable对象,最后使用getDrawable().getIntrinsicWidth()方法获取其固有宽度。
2. 直接在Java代码中创建一个Drawable对象,然后使用getDrawable().getIntrinsicWidth()方法获取其固有宽度,例如:
```
Drawable drawable = getResources().getDrawable(R.drawable.my_drawable);
int intrinsicWidth = drawable.getIntrinsicWidth();
```
这里的R.drawable.my_drawable是一个Drawable类型的资源,你可以替换成你自己的资源。
android studio getDrawable
在Android Studio 1.2.2中,当您编译代码时遇到Resources中getDrawable(int)已过时的警告信息时,这是由于Android Support库和AndroidX不兼容所引起的。为了解决这个问题,您可以使用以下方法之一:
方法一:使用ContextCompat.getDrawable()方法替代getDrawable()方法。
例如,将原来的代码:
getDrawable(R.drawable.my_image)
替换为:
ContextCompat.getDrawable(context, R.drawable.my_image)
方法二:将Android Support库迁移到AndroidX库。
您可以通过在build.gradle文件中将support库的依赖替换为相应的AndroidX库来进行迁移。
例如,将原来的依赖:
implementation 'com.android.support:appcompat-v7:28.0.0'
替换为:
implementation 'androidx.appcompat:appcompat:1.0.0'
以上是解决这个问题的两种方法。您可以根据您的需求选择其中一种方法来解决getDrawable(int)已过时的警告信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)