AppCompatPreferenceActivity

AppCompatPreferenceActivity

本文介绍了如何将工具栏添加到AppCompatPreferenceActivity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将工具栏添加到 AppCompatPreferenceActivity ,但我不知道该怎么做.

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

你能告诉我如何吗?

推荐答案

第一份副本 AppCompatPreferenceActivity 到您的项目.

First copy AppCompatPreferenceActivity to your project.

像这样使用它:

public class SettingsActivity extends AppCompatPreferenceActivity {

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

    private void setupActionBar() {
        Toolbar toolbar;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.list).getParent().getParent().getParent();
            toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.view_toolbar, root, false);
            root.addView(toolbar, 0);
        } else {
            ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
            ListView content = (ListView) root.getChildAt(0);
            root.removeAllViews();
            toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.view_toolbar, root, false);
            int height;
            TypedValue tv = new TypedValue();
            if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            } else {
                height = toolbar.getHeight();
            }
            content.setPadding(0, height, 0, 0);
            root.addView(content);
            root.addView(toolbar);
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

这不是一个非常优雅的解决方案,但它可以工作.经过姜饼和棒棒糖测试.

It's not a very elegant solution but it works. Tested on Gingerbread and Lollipop.

这篇关于如何将工具栏添加到AppCompatPreferenceActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:07