SwitchPreference
Androidでウィジェットのカスタムスタイルまたはその他の背景セレクターを描画可能に設定するにはどうすればよいですか?
(注:通常のウィジェットではなく、/で使用される標準ウィジェットSwitch
を意味します)SwitchPreference
PreferenceActivity
PreferenceFragment
SwitchPreference
Androidでウィジェットのカスタムスタイルまたはその他の背景セレクターを描画可能に設定するにはどうすればよいですか?
(注:通常のウィジェットではなく、/で使用される標準ウィジェットSwitch
を意味します)SwitchPreference
PreferenceActivity
PreferenceFragment
スイッチ自体のカスタム レイアウトを作成する必要があり、それを動的に適用することができます。
preference.setWidgetLayoutResource(R.layout.custom_switch);
しかし、詳細を説明し、これを実現する方法を正確に示します。
したがって、 preferences.xmlのような xml ファイルで設定を定義します。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="YOUR_CATEGORY_TITLE" >
<SwitchPreference
android:key="SWITCH"
android:title="YOUR_TITLE_FOR_SWITCH" />
</PreferenceCategory>
</PreferenceScreen>
次に、PreferenceActivty クラス内の onCreate() メソッドで読み取ります。
SwitchPreference pref = (SwitchPreference) findPreference(getString(R.string.SWITCH));
//pref.setChecked(true); // You can check it already if needed to true or false or a value you have stored persistently
pref.setWidgetLayoutResource(R.layout.custom_switch); // THIS IS THE KEY OF ALL THIS. HERE YOU SET A CUSTOM LAYOUT FOR THE WIDGET
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Here you can enable/disable whatever you need to
return true;
}
});
custom_switchレイアウトは次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textStyle="bold"
android:track="@drawable/switch_track"
android:thumb="@drawable/switch_thumb"/>
スイッチには、トラックとサムプロパティ用の 2 つのセレクターがあります。これらのセレクターのドローアブルは、tasomaniacによって提案された Android Holo Color Generator で生成できます。この場合、生成されたドローアブル フォルダーの内容をコピーするだけです (drawable-hdpi、drawable-mdpi、drawable-xhdpi、drawable-xxhdpi のみ)。ただし、必要な状態ごとにカスタム ビューを作成できます。
これらのセレクターは次のようになり ます。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_bg_focused" android:state_focused="true"/>
<item android:drawable="@drawable/switch_bg"/>
</selector>
スイッチサム:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_thumb_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/switch_thumb_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/switch_thumb_activated" android:state_checked="true"/>
<item android:drawable="@drawable/switch_thumb"/>
</selector>
そして、それはほとんどそれです。このソリューションは私を助けてくれました。何かを省略した場合はお知らせください。問題を修正します。
これを行う 1 つの方法は、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;
}
}