Java customlayout中switchpreference中on/off切换的颜色更改
我使用customlayout中的PreferenceScreen开发了一个设置页面。有一个我想改变颜色的开关。目前它采用默认颜色。但我想在用户打开和关闭它时更改它的颜色。当用户打开时,on侧应为红色,当用户切换到off侧时,“off”侧应为浅灰色。我的Switchpreference代码如下:Java customlayout中switchpreference中on/off切换的颜色更改,java,android,eclipse,android-layout,android-activity,Java,Android,Eclipse,Android Layout,Android Activity,我使用customlayout中的PreferenceScreen开发了一个设置页面。有一个我想改变颜色的开关。目前它采用默认颜色。但我想在用户打开和关闭它时更改它的颜色。当用户打开时,on侧应为红色,当用户切换到off侧时,“off”侧应为浅灰色。我的Switchpreference代码如下: <?xml version="1.0" encoding="utf-8"?> 我是android编程的新手,所以请合作。如果有人帮助我,我会很高兴。提前谢谢 试试这个: mVi
<?xml version="1.0" encoding="utf-8"?>
我是android编程的新手,所以请合作。如果有人帮助我,我会很高兴。提前谢谢 试试这个:
mViewSwitcher.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// set color here
return false;
}
});
我把这个答案贴在了一个类似的问题上 一种方法是将SwitchPreference子类化并重写onBindView方法。这样做时,您仍然希望在该方法中调用super.onBindView(视图),但随后在子视图中找到开关,并根据需要设置其样式:
package com.example;
import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import com.example.R;
public class CustomSwitchPreference extends SwitchPreference {
@SuppressLint("NewApi")
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
if (theSwitch!=null) {
//do styling here
theSwitch.setThumbResource(R.drawable.new_thumb_resource);
}
}
private Switch findSwitchInChildviews(ViewGroup view) {
for (int i=0;i<view.getChildCount();i++) {
View thisChildview = view.getChildAt(i);
if (thisChildview instanceof Switch) {
return (Switch)thisChildview;
}
else if (thisChildview instanceof ViewGroup) {
Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
if (theSwitch!=null) return theSwitch;
}
}
return null;
}
}
package.com.example;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.preference.SwitchPreference;
导入android.util.AttributeSet;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Switch;
导入com.example.R;
公共类CustomSwitchPreference扩展了SwitchPreference{
@SuppressLint(“新API”)
公共CustomSwitchPreference(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
}
公共CustomSwitchPreference(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共CustomSwitchPreference(上下文、属性集属性){
超级(上下文,attrs);
}
公共CustomSwitchPreference(上下文){
超级(上下文);
}
@凌驾
受保护的void onBindView(视图){
super.onBindView(视图);
切换开关=FindSwitchInChildView((视图组)视图);
如果(开关!=null){
//你在这里做造型吗
开关.setThumbResource(R.drawable.new\u thumb\u资源);
}
}
专用交换机FindSwitchInChildView(视图组视图){
对于(int i=0;iIt给出了一个名为的错误-类型的方法setOnDragListener(new View.OnDragListener(){})未定义SwitchPreference@android_newcomer你也可以试试这个解决方案
package com.example;
import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import com.example.R;
public class CustomSwitchPreference extends SwitchPreference {
@SuppressLint("NewApi")
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
if (theSwitch!=null) {
//do styling here
theSwitch.setThumbResource(R.drawable.new_thumb_resource);
}
}
private Switch findSwitchInChildviews(ViewGroup view) {
for (int i=0;i<view.getChildCount();i++) {
View thisChildview = view.getChildAt(i);
if (thisChildview instanceof Switch) {
return (Switch)thisChildview;
}
else if (thisChildview instanceof ViewGroup) {
Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
if (theSwitch!=null) return theSwitch;
}
}
return null;
}
}