如何从支持库中添加动作栏为preferenceActivity?库中、动作、preferenceActivity

由网友(清风撩心)分享简介:操作栏的兼容性已经被加入到支持库,修订18.现在有ActionBarActivity类在旧版本的Andr​​oid创建与行动吧。Action Bar compatibility has been added into support library, revision 18. It now has ActionBar...

操作栏的兼容性已经被加入到支持库,修订18.现在有ActionBarActivity类在旧版本的Andr​​oid创建与行动吧。

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

有没有办法从支持库中添加动作栏为 preferenceActivity

Is there any way to add Action Bar from support library into PreferenceActivity?

previously我用 ActionBarSherlock 并具有福尔摩斯preferenceActivity 。的

Previously I used ActionBarSherlock and it has SherlockPreferenceActivity.

推荐答案

编辑:在appcompat-V7 22.1.0谷歌增加了AppCompatDelegate抽象类作为一个代表,你可以用它来AppCompat的支持扩展到任何活动

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.

使用这样的:

...
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;
    }
}

没有更多的黑客攻击。 code摘自AppCompat$p$pferenceActivity.java.

No more hacking. Code taken from AppCompatPreferenceActivity.java.

阅读全文

相关推荐

最新文章