2013-12-15 1 views
3

Android의 Preference API를 사용하여 설정을 디자인했습니다.ActionBar의 마스터 켜기/끄기 스위치

하지만 "마스터 켜기/끄기 스위치"를 만드는 방법을 모르겠습니다. 여기에는 http://developer.android.com/design/patterns/settings.html이 언급되어 있지만 구현 방법에 대한 문서는 없습니다.

엄밀히 말하면 작업 표시 줄에있는 SwitchPreference는 모든 하위 설정을 해제했을 때 비활성화합니다.

아이디어가 있으십니까?

답변

6

수동으로 만들어야합니다 참조, 당신이 이것을 달성 할 수있는 스위치로 사용자 지정 작업 표시 줄을 생성해야합니다. 이것은 구현해야하는 패턴입니다. 작업 표시 줄에 버튼을 추가 할 수 있으며 클릭하면 해당 기능을 활성화 또는 비활성화하고 그에 따라 드로어 블을 변경할 수 있습니다. 당신은 스위치 태그를 사용할 수 있지만 이것은 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> 
+0

감사합니다. 를 사용할 수 있습니까? 그런 다음 자동으로 값을 저장합니다. – NoobieNoob

+0

다시 api에 추가됨 14. 귀하의 활동이 선호 활동입니까? –

+0

아니요, 정상적인 활동이며 "SettingsFragment extends PreferenceFragment"가 시작됩니다. 안드로이드가 자체 "설정"에서 어떻게하는지 궁금합니다. – NoobieNoob