0

Я сделал SettingsActivity с андроидом предпочтения.ПредпочтениеФрагментные кнопки (Предпочтение) не включаются, но CheckBoxPreference работают

В моем SettingsActivity я продлеваю SherlockFragmentActivity, поэтому я решил использовать PreferenceFragment.

Я сделал собственный класс, который расширяет PreferenceFragment, называемый GenericPreferenceFragment, который загружает мой предпочтительный xml из ресурса.

Предпочтения - 2 кнопки и 2 флажка.

Все работает хорошо в моем Nexus с последним обновлением Android, но в samsung с Android 4.2 кнопки не отображаются с поддержкой (они серые)!

Здесь экран:

android < 4.4 buttons not enabled

Почему это? Как я могу это исправить?


Код:

Вот часть моей com.xxx.yyy.SettingsActivity:

public class SettingsActivity extends SherlockFragmentActivity implements 
     OnPreferenceClickListener, OnClickListener { 

    private final String TAG = "SETTINGS"; 
    private ImageButton homeButton; 
    private ImageButton settingsButton; 
    private TextView activityTitle; 

    private ProgressDialog pDialog; 
    private GenericPreferenceFragment gPrefFragment; 

    private CheckBoxPreference cbpGCM; 
    private CheckBoxPreference cbpMail; 
    private Preference pKeywords; 
    private Preference pLogout; 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     return super.onOptionsItemSelected(item); 
    } 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     setupActionBar(); 
     gPrefFragment = (GenericPreferenceFragment) getFragmentManager().findFragmentById(R.id.pref_frag_id); 

     FontsOverride.setDefaultFont(getApplicationContext(), "DEFAULT", 
       "OpenSans-Regular.ttf"); 
     layoutInitializer(); 
    } 

    // @Override 
    // public void onBuildHeaders(List<Header> target) { 
    // loadHeadersFromResource(R.xml.preference_headers, target); 
    // } 

    private void setupActionBar() { 
     getSupportActionBar(); 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.custom_abs_layout); 
    } 

    private void layoutInitializer() { 
     pDialog = DialogUtil.makeDialog(SettingsActivity.this); 

     // --- ACTIONBAR --- 
     homeButton = (ImageButton) findViewById(R.id.button_menu_home); 
     settingsButton = (ImageButton) findViewById(R.id.button_menu_settings); 
     activityTitle = (TextView) findViewById(R.id.tv_activity_title); 
     activityTitle.setText(R.string.title_settings); 

     homeButton.setBackgroundResource(0); 
     settingsButton.setBackgroundResource(0); 
     homeButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_return)); 


     cbpGCM = (CheckBoxPreference) gPrefFragment.findPreference("pref_device_notification"); 
     cbpMail = (CheckBoxPreference) gPrefFragment.findPreference("pref_mail_notification"); 
     pKeywords = (Preference) gPrefFragment.findPreference("pref_keyword"); 
     pLogout = (Preference) gPrefFragment.findPreference("pref_logout"); 

     pKeywords.setOnPreferenceClickListener(this); 
     pLogout.setOnPreferenceClickListener(this); 
     cbpMail.setOnPreferenceClickListener(this); 
     cbpGCM.setOnPreferenceClickListener(this); 

     homeButton.setOnClickListener(this); 


     // set button on/off 
      ... 
     cbpGCM.setChecked(gcmStatus); 
     cbpMail.setChecked(mailStatus); 
    } 

    @Override 
    public boolean onPreferenceClick(Preference preference) { 
     String key = preference.getKey(); 
     if (key.equalsIgnoreCase("pref_keyword")) { 
      finish(); 
      startActivity(...); 
     } else if (key.equalsIgnoreCase("pref_logout")) { 
      getLogout(); 
     }else if (key.equalsIgnoreCase("pref_device_notification")) { 
      boolean status = cbpGCM.isChecked(); 
      switchGCM(status); 
     } else if (key.equalsIgnoreCase("pref_mail_notification")) { 
      boolean status = cbpMail.isChecked(); 
      switchMail(status); 
     } 

     return false; 
    } 
} 

Это R.layout.activity_settings:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_margin="10dp" 
     android:background="#" 
     android:orientation="vertical" > 

     <fragment 
      android:id="@+id/pref_frag_id" 
      android:name="com.xxx.yyy.GenericPreferenceFragment" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

</LinearLayout> 

Это мой com.xxx.yyy.GenericPreferenceFragment:

public class GenericPreferenceFragment extends PreferenceFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

    } 
} 

И это R.xml.preferences:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#" > 

    <PreferenceCategory 
     android:key="pref_keyword_settings" 
     android:title="@string/label_keyword_settings" > 
     <Preference 
      android:dependency="pref_keyword_settings" 
      android:icon="@drawable/ic_keyword_settings" 
      android:key="pref_keyword" 
      android:summary="@string/description_keyword_preference" 
      android:title="@string/title_keyword_preference" /> 
    </PreferenceCategory> 


    <PreferenceCategory 
     android:key="pref_notification_settings" 
     android:title="@string/label_notification_settings" > 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:icon="@drawable/ic_notification_settings" 
      android:key="pref_device_notification" 
      android:summary="@string/description_device_notification" 
      android:title="@string/title_device_notification" /> 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:icon="@drawable/ic_mail_settings" 
      android:key="pref_mail_notification" 
      android:summary="@string/description_mail_notification" 
      android:title="@string/title_mail_notification" /> 
    </PreferenceCategory> 


    <PreferenceCategory 
     android:key="pref_logout_settings" 
     android:title="@string/label_logout_settings" > 
     <Preference 
      android:dependency="pref_logout_settings" 
      android:icon="@drawable/ic_logout" 
      android:key="pref_logout" 
      android:enabled="true" 
      android:summary="@string/description_logout_preference" 
      android:title="@string/title_logout_preference" /> 
    </PreferenceCategory> 

</PreferenceScreen> 

ответ

0

После некоторых attemps я получил решение: Я должен удалить android:dependency = "pref_keyword_settings" и android:dependency = "pref_logout_settings" от R.xml.preferences. С зависимостью Preferences.isEnable() результат всегда был false!