【问题标题】:How to add ToolBar in PreferenceActivity?如何在 PreferenceActivity 中添加 ToolBar?
【发布时间】:2015-07-20 07:08:23
【问题描述】:

我想在我的 android 应用程序中将 ToolBar 添加到 PreferenceActivity 中。我写了以下代码。

  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();
        }
    });

}

这在我的 android Kitkat 手机 API 19 中完美运行,但在 API 级别 10 中强制关闭,即 gingerbread。请给我建议。

【问题讨论】:

    标签: android toolbar preferenceactivity


    【解决方案1】:

    您需要一个包含ToolbarListViewandroid:id="@android:id/list" 的布局

    activity_settings.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>
    

    SettingsActivity.java

    public class SettingsActivity extends PreferenceActivity {
    
        private AppCompatDelegate mDelegate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            getDelegate().installViewFactory();
            getDelegate().onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
            setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
            addPreferencesFromResource(R.xml.preferences);
            ...
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            getDelegate().onPostCreate(savedInstanceState);
        }
    
        @Override
        public void setContentView(@LayoutRes int layoutResID) {
            getDelegate().setContentView(layoutResID);
        }
    
        @Override
        protected void onPostResume() {
            super.onPostResume();
            getDelegate().onPostResume();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            getDelegate().onStop();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            getDelegate().onDestroy();
        }
    
        private void setSupportActionBar(@Nullable Toolbar toolbar) {
            getDelegate().setSupportActionBar(toolbar);
        }
    
        private AppCompatDelegate getDelegate() {
            if (mDelegate == null) {
                mDelegate = AppCompatDelegate.create(this, null);
            }
            return mDelegate;
        }
        ...
    }
    

    查看我的完整工作示例:

    来自 Android 团队的参考资料:AppCompatPreferenceActivity

    【讨论】:

    • Hidro,为什么布局需要 ListView?
    • PreferenceActivity 需要 ListViewandroid:id/list 如果您覆盖布局,这就是原因。
    • 现在我可以让它工作了。谢谢@hidro 的解释。
    • 对我不起作用...它确实显示了工具栏,但它在首选项布局下。
    • 在 Android 4.4 上,我从列表中丢失了填充,因此活动看起来不像原生。
    【解决方案2】:

    试试:

    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_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>
    

    【讨论】:

    • 什么是 AppCompatPreferenceActivity ?
    • @Eslam,它是一个扩展 Preference Activity 的类。 'New' -> 'Settings Activity' 会显示出来。
    • @style/AppTheme.AppBarOverlay@style/AppTheme.PopupOverlay 文件中有什么?
    【解决方案3】:

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

    转到AndroidManifest.xml 并在您的偏好活动中添加自定义theme

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

    这是添加到上述@style/Pref的自定义主题

     <style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    希望对你有帮助!

    【讨论】:

      【解决方案4】:

      您可以从@android:style/Theme.Material.Light.DarkActionBar轻松添加工具栏

      AndroidManifest.xml 中:

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

      v21/styles.xml

      <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 中用于 Back API 支持

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

      【讨论】:

        【解决方案5】:

        在我的情况下,通过扩展自定义工具栏非常简单且运行良好。 在您的 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 端代码中,在顶部添加一个偏好类别,如下所示,

          <?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>
        

        在你的布局资源文件夹toolbar_setting中,

           <?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" />
        

        【讨论】:

          【解决方案6】:

          代替:

          public class PreferencesActivity extends Activity
          

          这样做:

          public class PreferencesActivity extends AppCompatActivity
          

          【讨论】:

            【解决方案7】:

            此方法无法解决添加工具栏的问题,但您可以为您的设置页面恢复默认的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>
            

            在 AndroidManifest.xml 中设置android:theme

            <activity
                android:name=".Settings"
                android:theme="@style/AppTheme.Settings" />
            

            仅此而已。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-08-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-24
              • 1970-01-01
              • 2023-03-10
              • 1970-01-01
              相关资源
              最近更新 更多