在首选项活动中添加工具栏?

时间:2017-08-20 12:14:19

标签: java android

我已使用偏好设置创建了应用设置。但我注意到,我的PreferenceActivity中没有工具栏。如何将工具栏添加到我的PreferenceActivity?

public class SettingsActivity extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener {

    PreferenceManager manager;
    ListPreference listPreference;
    SharedPreferences sharedPreference;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.language_option_preference);
        sharedPreference = PreferenceManager.getDefaultSharedPreferences(this);

        manager = getPreferenceManager();
        listPreference = (ListPreference) manager.findPreference("language_setting");

        listPreference.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        Resources resource = getResources();
        Configuration config = resource.getConfiguration();

        int pos = Integer.parseInt((String) newValue);
        if (pos == 1) {
            sharedPreference.edit().putString("language", "en").commit();
            config.locale = Locale.ENGLISH;
            listPreference.setValue("1");
        } else if (pos == 2) {
            sharedPreference.edit().putString("language", "zh").commit();
            config.locale = Locale.CHINA;
            listPreference.setValue("2");
        } else {
            sharedPreference.edit().putString("language", "auto").commit();
            config.locale = Locale.getDefault();
            listPreference.setValue("0");
        }

        getBaseContext().getResources().updateConfiguration(config, null);

        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

获取AppCompatPreferenceActivity并将其添加到您的项目中。然后:

ActionBar bar = getSupportActionBar();
if( bar != null ) {
    // remove back arrow
    bar.setDisplayHomeAsUpEnabled( false );
    bar.setDisplayShowTitleEnabled( true );
}

我强烈建议您阅读this文章,了解如何使用Preferences / PreferenceFragment。

答案 1 :(得分:0)

让你的布局像这样......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/content_frame"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />
</LinearLayout>

在onCreate方法

中添加工具栏
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

来自参考: https://chromium.googlesource.com/android_tools/+/7200281446186c7192cb02f54dc2b38e02d705e5/sdk/extras/android/support/samples/Support7Demos/src/com/example/android/supportv7/app/AppCompatPreferenceActivity.java