PreferenceActivity中的SetSupportActionbar?

时间:2014-11-20 14:16:52

标签: android android-appcompat

我刚刚编写了一个使用v21工具栏而不是原始ActionBar的应用程序。它的工作原理是将工具栏添加为" setSupportActionBar()"。

现在我已经创建了一个PreferenceActivity,问题是这个Activity没有显示任何ActionBar或工具栏。如何将SupportActionBar添加到PreferenceActivity? 这是我的基本主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>

正如您可以看到正常的windowActionbar已关闭。

2 个答案:

答案 0 :(得分:0)

您可以在ActionBarActivity中创建PreferenceFragment。

答案 1 :(得分:0)

我也在努力解决这个问题。我最终给工具栏充气并将其添加到根布局(https://stackoverflow.com/a/27455330/1889752)。

PreferenceActivity

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

    LinearLayout linearLayout = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.tool_bar, linearLayout, false);

    toolbar.setTitle(getString(R.string.settings_header_title));
    linearLayout.addView(toolbar, 0);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            overridePendingTransition(0,0);
            finish();
        }
    });
}

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/> 

tool_bar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:navigationIcon="?attr/homeAsUpIndicator">

您应该查看AppCompatPreferenceActivity

相关问题