【问题标题】:Color change of on/off toggle in switchpreference in customlayout自定义布局中开关首选项中开/关切换的颜色变化
【发布时间】:2015-02-26 12:49:11
【问题描述】:

我在 customlayout 中使用 PreferenceScreen 开发了一个设置页面。有一个我想改变颜色的开关偏好。目前它采用默认颜色。但是我想在用户打开和关闭它时更改它的颜色。当用户打开时,打开侧应该是红色,当用户切换到关闭侧时,“关闭”侧应该是浅灰色。我的 Switchpreference 代码如下:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory android:key="pref" >

    <SwitchPreference
        android:key="switchbutton"
        android:summaryOff="OFF"
        android:summaryOn="ON"
        android:title="start" />
</PreferenceCategory>

我是android编程的新手,所以请合作。如果有人帮助我,我会很高兴。在此先感谢!

【问题讨论】:

    标签: java android eclipse android-layout android-activity


    【解决方案1】:

    我在Custom SwitchPreference in Android的类似问题中发布了这个答案

    这样做的一种方法是继承 SwitchPreference 并覆盖 onBindView 方法。这样做时,您仍然希望在该方法中调用 super.onBindView(view),然后在子视图中找到 Switch 并根据需要设置其样式:

    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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      mViewSwitcher.setOnDragListener(new View.OnDragListener() {
      @Override
      public boolean onDrag(View v, DragEvent event) {
      // set color here
      return false;
      }
      });
      

      【讨论】:

      • 它给出了一个错误,称为 - 对于 SwitchPreference 类型的方法 setOnDragListener(new View.OnDragListener(){}) 是未定义的
      • @android_newcomer 你也可以试试这个解决方案stackoverflow.com/a/21854548
      猜你喜欢
      • 1970-01-01
      • 2020-04-14
      • 2015-01-24
      • 2020-03-21
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多