How to add toolbars to AppCompatPreferenceActivity?

后端 未结 6 836
一生所求
一生所求 2020-12-31 04:42

I\'m trying to add toolbars to the AppCompatPreferenceActivity but I don\'t know how to do so.

Can you tell me how?

6条回答
  •  别那么骄傲
    2020-12-31 05:36

    Use PreferenceFragment, You can do like this in your activity:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new MyPreferenceFragment()).commit();
    

    And the MyPreferenceFragment like this:

    public class MyPreferenceFragment extends PreferenceFragment{
        @Override
        public void onCreate(final Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
        }
    }
    

    Hope this helped!

提交回复
热议问题