我尝试使用以下内容制作xml选择器:
backgroundDrawable
当我尝试将checkBox
设置为CheckBox
时,复选框也不会替换 shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));
样式:
button drawable
关注此问题:Change icons of checked and unchecked for Checkbox for Android我需要将android:button="@drawable/checkbox"
设置为我的xml:CheckBox
,但我无法执行此操作,因为我正在以编程方式创建Shader "Custom/NoobShader_03" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Scale ("Wave Scale", float) = 0.6
_Frequency ("Frequency", float) = 1
_Speed ("Speed", float) = 0.5
_Scale2 ("Wave Scale", float) = 0.6
_Frequency2 ("Frequency", float) = 1
_Speed2 ("Speed", float) = 1
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
float _Scale;
float _Frequency;
float _Speed;
float _Scale2;
float _Frequency2;
float _Speed2;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float3 nor : NORMAL;
float4 col : COLOR;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
float randomNum = 1 / _Time;
float newPos = _Scale * sin(_Time.w * _Speed + i.vertex.x * _Frequency);
float newPos2 = _Scale2 * cos(_Time.w * _Speed2 + i.vertex.z * _Frequency2);
//i.vertex.y += newPos;
float4 tempPos = i.vertex;
tempPos.y += newPos * newPos2;
VOUT.pos = mul(UNITY_MATRIX_MVP,tempPos);
VOUT.nor = float3(mul(UNITY_MATRIX_MVP,tempPos).xyz);
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _World2Object).xyz);
float3 lightDirection;
float atten = 1.0;
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuseRefflection = atten * _LightColor0.xyz * _Color.rgb * max( 0.0, dot(normalDirection, lightDirection));
VOUT.col = float4(diffuseRefflection, 0.0);
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
FOUT.color = v.col + _Color;
return FOUT;
}
ENDCG
}
}
FallBack "Diffuse"
}
。
有没有办法如何实现这个目标?
答案 0 :(得分:14)
在drawable文件夹中创建一个新布局并将其命名为custom_checkbox(您也可以重命名)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/Checked_image_name"android:state_checked="true"/>
<item android:drawable="@drawable/Unchecked_image_name" android:state_checked="false"/>
</selector>
在布局活动
中使用此功能<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_checkbox"/>
答案 1 :(得分:9)
使用下面的代码行,我认为它会起作用
yourcheckbox.setButtonDrawable(yourdrawable);
答案 2 :(得分:4)
use this
shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radiobutton_checked" android:state_checked="true"/>
<item android:drawable="@drawable/radiobutton_unchecked" android:state_checked="false"/>
</selector>
答案 3 :(得分:0)
这对我有用:
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_checkbox"
android:button="@null"/>
答案 4 :(得分:0)
如果您使用的是sdk 28(androidx)及更高版本,请尝试使用
androidx.appcompat.widget.AppCompatCheckBox
代替
Checkbox
答案 5 :(得分:0)
drawable / checkbox_custome.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/checkbox_empty" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_check" />
</selector>