更改子(嵌套)PreferenceScreen标题颜色

时间:2014-10-28 06:35:41

标签: android android-actionbar sharedpreferences

我用过这个:

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    addPreferencesFromResource(R.xml.prefs);
    final PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("child");
    preferenceScreen.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            // TODO Auto-generated method stub
            PreferenceScreen a = (PreferenceScreen) preference;
            a.getDialog().getWindow().setTitle("NewTitle");
            a.getDialog().getWindow().setTitleColor(getResources().getColor(R.color.orange));
            preferenceScreen.getDialog().getActionBar().setDisplayShowHomeEnabled(false);
            preferenceScreen.getDialog().getActionBar().setBackgroundDrawable(null);
            return true;
        }
    });

}

它只是将标题更改为NewTitle并隐藏ActionBar标题下方的蓝线而不更改titlecolor。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个

  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    super.onPreferenceTreeClick(preferenceScreen, preference);

    if (preference != null) {
        if (preference instanceof PreferenceScreen) {
             initializeActionBar((PreferenceScreen) preference);
                    }
        }
    }

    return false;
}

,这是initializeActionBar:

       public static void initializeActionBar(PreferenceScreen preferenceScreen) 
          {

      int titleId;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          titleId = getResources().getIdentifier("action_bar_title", "id", "android");
           } else {
                titleId = R.id.action_bar_title;
                  }    
        TextView titleTextView=(TextView) dialog.findViewById(titleId);
        if(titleTextView!=null)
        titleTextView.setTextColor(Color.WHITE);

}