该Resources.getColor(int id)方法已被弃用.
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
android android-resources android-mnc android-6.0-marshmallow
使用:
buildToolsVersion "22.0.1",
targetSdkVersion 22在我的gradle文件中.
我发现有用的getResources().getColor(R.color.color_name)是不推荐使用的.
我应该用什么呢?
Button当我使用android:backgroundxml中的属性为其设置背景时,我遇到了视图问题.在设置背景之前,该按钮具有默认大小.但是,当我将颜色作为背景,这是挺大的,虽然我没有设置不同的width或者height在其上.
在将background属性设置为button之前,这是我的xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_row_option_done"
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Edit"
android:id="@+id/btn_row_option_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Delete"
android:id="@+id/btn_row_option_delete"
android:background="@color/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
所以取消按钮是默认大小,如截图.
但是当我在这样的取消按钮上设置颜色时
<Button
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
然后按钮变得比默认大小更大,即使我没有使宽度或高度更大.
这是截图
如上所示,取消按钮变大了.其实我正在设置背景颜色.即使在设置背景颜色后,如何修复它以获得默认大小?
我使用以下行更改VectorDrawable的颜色:
mydrawable.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
尽管现在已不推荐使用,但效果很好。该文档建议我使用:
mydrawable.getBackground().setColorFilter(new BlendModeColorFilter(color, PorterDuff.Mode.SRC_ATOP))
虽然,BlendModeColorFilter仅在API29上可用。在检查了不赞成使用的方法的源之后,我意识到它调用了:
new PorterDuffColorFilter()
因此,我继续使用:
mydrawable.getBackground().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP))
着色工作。这是不推荐使用的方法的正确替代方法,还是我必须在API29上使用BlendModeColorFilter?
谢谢。