수동으로 만들어야합니다 참조, 당신이 이것을 달성 할 수있는 스위치로 사용자 지정 작업 표시 줄을 생성해야합니다. 이것은 구현해야하는 패턴입니다. 작업 표시 줄에 버튼을 추가 할 수 있으며 클릭하면 해당 기능을 활성화 또는 비활성화하고 그에 따라 드로어 블을 변경할 수 있습니다. 당신은 스위치 태그를 사용할 수 있지만 이것은 api 14에서만 소개됩니다.
:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/switch_layout"
/>
</menu>
당신이 사용이뿐만 아니라
public class TogglePreference extends Preference {
public TogglePreference(Context context) {
super(context);
}
public TogglePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TogglePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View getView(View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new LinearLayout(getContext());
((LinearLayout) convertView)
.setOrientation(LinearLayout.HORIZONTAL);
TextView txtInfo = new TextView(getContext());
txtInfo.setText("Test");
((LinearLayout) convertView).addView(txtInfo,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
ToggleButton btn = new ToggleButton(getContext());
((LinearLayout) convertView).addView(btn);
}
return convertView;
}
그리고 preferences.xml로를 추가 할 수 있습니다를 다음과 같이 예,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
그런 다음 mainmenu.xml에 항목을 추가
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Test custom preferences" >
<android.dumdum.TogglePreference />
</PreferenceCategory>
</PreferenceScreen>
첫 번째 링크에 감사드립니다. 이것은 나를 위해 그리고 내가 원하는대로 작동합니다. +1 – Amir