How to add Action Bar from support library into PreferenceActivity?

后端 未结 8 656
不思量自难忘°
不思量自难忘° 2020-11-27 09:15

Action Bar compatibility has been added into support library, revision 18. It now has ActionBarActivity class for creating activities with Action Bar on older versions of An

8条回答
  •  迷失自我
    2020-11-27 09:53

    EDIT: In appcompat-v7 22.1.0 Google added the AppCompatDelegate abstract class as a delegate you can use to extend AppCompat's support to any activity.

    Use it like this:

    ...
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatDelegate;
    import android.support.v7.widget.Toolbar;
    ...
    
    public class SettingsActivity extends PreferenceActivity {
    
        private AppCompatDelegate mDelegate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            getDelegate().installViewFactory();
            getDelegate().onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            getDelegate().onPostCreate(savedInstanceState);
        }
    
        public ActionBar getSupportActionBar() {
            return getDelegate().getSupportActionBar();
        }
    
        public void setSupportActionBar(@Nullable Toolbar toolbar) {
            getDelegate().setSupportActionBar(toolbar);
        }
    
        @Override
        public MenuInflater getMenuInflater() {
            return getDelegate().getMenuInflater();
        }
    
        @Override
        public void setContentView(@LayoutRes int layoutResID) {
            getDelegate().setContentView(layoutResID);
        }
    
        @Override
        public void setContentView(View view) {
            getDelegate().setContentView(view);
        }
    
        @Override
        public void setContentView(View view, ViewGroup.LayoutParams params) {
            getDelegate().setContentView(view, params);
        }
    
        @Override
        public void addContentView(View view, ViewGroup.LayoutParams params) {
            getDelegate().addContentView(view, params);
        }
    
        @Override
        protected void onPostResume() {
            super.onPostResume();
            getDelegate().onPostResume();
        }
    
        @Override
        protected void onTitleChanged(CharSequence title, int color) {
            super.onTitleChanged(title, color);
            getDelegate().setTitle(title);
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            getDelegate().onConfigurationChanged(newConfig);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            getDelegate().onStop();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            getDelegate().onDestroy();
        }
    
        public void invalidateOptionsMenu() {
            getDelegate().invalidateOptionsMenu();
        }
    
        private AppCompatDelegate getDelegate() {
            if (mDelegate == null) {
                mDelegate = AppCompatDelegate.create(this, null);
            }
            return mDelegate;
        }
    }
    

    No more hacking. Code taken from AppCompatPreferenceActivity.java.

提交回复
热议问题