android – AppCompat的AccountAuthenticatorActivity

我正在按照教程制作验证器:http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

登录Activity需要扩展AccountAuthenticatorActivity,问题从这里开始:AccountAuthenticatorActivity扩展常规Activity而不是AppCompatActivity.

使用AppCompat中的常规Activity会导致没有ActionBar的Activity.我想使用AccountAuthenticatorActivity并拥有一个ActionBar.

解决方法:

关键是AppCompatDelegate,我的代码基于Android Studio生成的AppCompatPreferenceActivity类:

@SuppressWarnings("unused")
public class AppCompatAuthActivity extends AccountAuthenticatorActivity {

    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
    @NonNull
    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;
    }

}

AppCompatDelegate是将ActionBar添加到任何常规Activity(例如PreferenceActivity)的关键.

不要忘记您的活动必须扩展AppCompatAuthActivity.

上一篇:UVA 11021 C - Tribles(概率DP)


下一篇:android – 找不到与给定名称’Theme.AppCompat.Light.DarkActionBar’匹配的资源