Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 安卓-更改抽绳的颜色_Android_Android Drawable - Fatal编程技术网

Android 安卓-更改抽绳的颜色

Android 安卓-更改抽绳的颜色,android,android-drawable,Android,Android Drawable,我有一个选择器,其中我设置了一个圆作为状态_selected=true的背景,但我想在单击对象时更改颜色。我怎么做 以下是我的抽屉的设置方式: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#D0021B"/> <stroke android:width="2dp" android:c

我有一个选择器,其中我设置了一个圆作为状态_selected=true的背景,但我想在单击对象时更改颜色。我怎么做

以下是我的抽屉的设置方式:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#D0021B"/>
<stroke android:width="2dp" android:color="#910012" />
<size
    android:width="50dp"
    android:height="50dp" />
<corners android:radius="50dp" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black" android:state_selected="false" />
<item android:drawable="@drawable/nav_circle" android:state_selected="true" />
</selector>

如果要在按下时更改颜色,可以使用state\u pressed

<item android:state_pressed="true"
      android:drawable="@color/pressedColor"/> <!-- pressed state -->
<item android:state_focused="true"
      android:drawable="@color/pressedColor"/> <!-- focused state -->
<item android:drawable="@color/colorPrimary"/>

也许这会帮助你:

i=(ImageView)findViewById(R.id.image);
    i.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            i.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP); 
        }
    });
ImageView xml代码:

<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/nav_circle"
   android:layout_centerVertical="true"
   android:layout_centerHorizontal="true"
   android:id="@+id/image" />

您可以通过编程实现:

public static void colorDrawable(View view, int color) {
            Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
            if (wrappedDrawable != null) {
                DrawableCompat.setTint(wrappedDrawable.mutate(), color);
                setBackgroundDrawable(view, wrappedDrawable);
            }
        }

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackgroundDrawable(View view, Drawable drawable) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(drawable);
    } else {
        view.setBackground(drawable);
    }
}
publicstaticvoidcolorDrawable(视图,int-color){
Drawable WrappedAddressable=DrawableCompat.wrap(view.getBackground());
如果(WrappedAddressable!=null){
DrawableCompat.setTint(wrappedravable.mutate(),color);
可缩进地面绘制(视图,可包装);
}
}
@TargetApi(Build.VERSION\u code.JELLY\u BEAN)
公共静态空间后退可绘制(视图,可绘制){
if(Build.VERSION.SDK\u INT
使用state\u pressed…..如何将颜色设置为动态?我得到的是android.graphics.drawable.StateListDrawable无法强制转换为android.graphics.drawable.GradientDrawable错误您可以不强制转换就这样尝试:
i.getBackground().setColorFilter(color.BLUE,PorterDuff.Mode.SRC\u)
还可以查看我的xml代码吗?我需要能够设置来自服务器的动态颜色
<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/nav_circle"
   android:layout_centerVertical="true"
   android:layout_centerHorizontal="true"
   android:id="@+id/image" />
public static void colorDrawable(View view, int color) {
            Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
            if (wrappedDrawable != null) {
                DrawableCompat.setTint(wrappedDrawable.mutate(), color);
                setBackgroundDrawable(view, wrappedDrawable);
            }
        }

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackgroundDrawable(View view, Drawable drawable) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(drawable);
    } else {
        view.setBackground(drawable);
    }
}