繁体   English   中英

将工具栏/操作栏添加到PreferenceActivity

[英]Add Toolbar/Actionbar to PreferenceActivity

我正在尝试向我的PreferenceActivity添加工具栏。 我到这里找了很多地方,似乎什么也没有。

我在顶层使用标题。 然后,我尝试在onCreate中使用自己的布局,包括所需的工具栏,并确保其具有名为“ @android:id / list”的视图。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:title="@string/app_name"
            app:navigationIcon="@drawable/back"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

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


</LinearLayout>

这对于顶层来说还可以,但是下一层在寻找视图名称“ android:id / prefs”时失败了。

java.lang.IllegalArgumentException: No view found for id 0x102040a (android:id/prefs) for fragment PreferencesFragmentButtonInteraction{7a6584a #0 id=0x102040a}

该片段的onCreate中唯一的代码是AddPreferencesFromResource。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences_button_interaction);
}

我尝试使用主题并设置在清单中,但无济于事。

您可以使用包装在普通AppCompatActivity中的PreferenceFragmentCompat。 然后,您可以使用以下方式轻松将应用栏附加到活动中

setContentView(R.layout.activity_setting);

可以使用以下方式将首选项设置为片段

setPreferencesFromResource(R.xml.preferences_button_interaction, rootKey);

如果希望将工具栏添加到当前活动中,可以尝试此方法

private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat

if (rootView != null) {
    View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
    rootView.addView(view, 0);

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    // Show the Up button in the action bar.
    actionBar.setDisplayHomeAsUpEnabled(true);
}
}

app_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM