Android 如何在PreferenceActivity中添加工具栏?

Android 如何在PreferenceActivity中添加工具栏?,android,toolbar,preferenceactivity,Android,Toolbar,Preferenceactivity,我想在android应用程序的首选项活动中添加工具栏。我编写了以下代码 public class SettingsActivity extends PreferenceActivity { SendSMS sms; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings

我想在android应用程序的
首选项活动中添加
工具栏
。我编写了以下代码

  public class SettingsActivity extends PreferenceActivity  {
 SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
    root.addView(bar, 0);
    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
}


这在我的androidKitkat手机API 19中运行得非常好,但在API级别10时强制关闭,即姜饼。请建议我。

您需要一个包含
工具栏
列表视图
以及
android:id=“@android:id/list”

活动设置.xml

<?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" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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>
<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
查看我的完整工作示例:

Android团队的参考资料:

试试:

public class SettingsActivity extends AppCompatPreferenceActivity {
.....

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();
    /* getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();
    */

    //addPreferencesFromResource(R.xml.pref_general);        
}

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\u bar\u layout.xml

<?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" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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>
<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>

您可以从
@android:style/Theme.Material.Light.darkaActionBar

在AndroidManifest.xml中:

<activity
    android:name=".activity.SettingsActivity"
    android:theme="@style/SettingsTheme"
    android:label="Settings"/>

v21/styles.xml中

<?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" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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>
<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>

@颜色/原色
@颜色/原色暗

v14/styles.xml中获取后台API支持

<style name="SettingsTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar.V14.Movie.NoTitle</item>

@style/ActionBar.V14.Movie.NoTitle
而不是:

public class PreferencesActivity extends Activity
这样做:

public class PreferencesActivity extends AppCompatActivity

非常简单,在我的情况下,通过膨胀自定义工具栏工作良好。 在java代码中,执行以下操作:

   public class SettingsPrefActivity extends AppCompatPreferenceActivity {
        //  private static final String TAG = SettingsPrefActivity.class.getSimpleName();

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

            //setting up toolbar
            getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
            Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.setTitle("Settings");
            setSupportActionBars(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // load settings fragment
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
        }

    }
  <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
    <PreferenceCategory android:layout="@layout/toolbar_setting"></PreferenceCategory>
在xml端代码的顶部添加一个首选项类别,如下所示:

   public class SettingsPrefActivity extends AppCompatPreferenceActivity {
        //  private static final String TAG = SettingsPrefActivity.class.getSimpleName();

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

            //setting up toolbar
            getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
            Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.setTitle("Settings");
            setSupportActionBars(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // load settings fragment
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
        }

    }
  <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
    <PreferenceCategory android:layout="@layout/toolbar_setting"></PreferenceCategory>

//将下一行放在xml首选项布局屏幕的顶部。。
并在布局资源文件夹工具栏的设置中

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/appbar_elevation"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

我迟到了,但这里有一个解决问题的小方法,而不是编写大量代码或添加庞大的库

转到
AndroidManifest.xml
并在首选项活动中添加自定义
主题

 <activity
        android:name=".Dashboard.PreferencepActivity"
        android:label="@string/title_activity_preference"
        android:theme="@style/Pref"></activity>
<activity
    android:name=".Settings"
    android:theme="@style/AppTheme.Settings" />

希望有帮助

此方法无法解决添加工具栏的问题,但您可以将默认的
ActionBar
返回到设置页面。通过继承父主题,为“设置”活动创建主题

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.Settings" parent="AppTheme">
    <item name="windowNoTitle">false</item>
    <item name="windowActionBar">true</item>
</style>

就这些。

Hidro,为什么版面需要一个列表视图?
PreferenceActivity
需要一个
ListView
android:id/list
在版面中,如果覆盖版面,这就是为什么。现在我可以让它工作了。谢谢你,@hidro,谢谢你的解释。对我来说没用。。。它确实显示了工具栏,但它位于首选项布局下。在Android 4.4上,我从列表中去掉了填充,因此该活动看起来不像本机活动。什么是AppCompatPreferenceActivity?@Eslam,它是一个扩展首选项活动的类新的'->'设置活动'将显示出来。您在
@style/AppTheme.AppBarOverlay
@style/AppTheme.PopupOverlay
文件中有什么?