Android AccountAuthenticatorActivity中未显示操作栏和菜单

Android AccountAuthenticatorActivity中未显示操作栏和菜单,android,android-actionbar,android-authenticator,Android,Android Actionbar,Android Authenticator,由于我在应用程序中使用操作栏而不是菜单,因此在AccountAuthenticatorActivity的实现中不会显示任何操作栏或/菜单。其他活动显示动作栏没有问题 我不确定这是一个bug还是我这边的代码缺少修改。有人经历过同样的问题吗?我没有发现与此问题相关的任何其他问题 public class MyAuthenticatorActivity extends AccountAuthenticatorActivity { 在许多其他活动中使用的XML菜单应该是正确的: <?xml v

由于我在应用程序中使用操作栏而不是菜单,因此在AccountAuthenticatorActivity的实现中不会显示任何操作栏或/菜单。其他活动显示动作栏没有问题

我不确定这是一个bug还是我这边的代码缺少修改。有人经历过同样的问题吗?我没有发现与此问题相关的任何其他问题

public class MyAuthenticatorActivity extends AccountAuthenticatorActivity {

在许多其他活动中使用的XML菜单应该是正确的:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menuHome"
        android:icon="@drawable/ic_menu_home"
        android:orderInCategory="110"
        android:showAsAction="ifRoom"
        android:title="@string/menu_home"/>
    <item
        android:id="@+id/menuSettings"
        android:icon="@drawable/ic_menu_settings"
        android:orderInCategory="111"
        android:showAsAction="ifRoom"
        android:title="@string/menu_settings"/>
    <item
        android:id="@+id/menuInfo"
        android:icon="@drawable/ic_menu_info"
        android:orderInCategory="113"
        android:showAsAction="ifRoom"
        android:title="@string/menu_info"/>

</menu>

我知道我的答案晚了。但我能在这个基础上绕过这个基地。由于某种原因,
AccountAuthenticatorActivity
似乎没有真正的
ActionBar
。特别是,当您想要使用支持库时,这确实很难处理

我是这样做的:

您必须使用
坐标布局将您的活动布局括起来

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorlayout_homescreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_login"
    tools:context="com....LoginActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbarlayout_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?android:attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="64dp"
            app:expandedTitleMarginEnd="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        ...
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
这将确保您的
工具栏在向上滚动时不会消失/折叠,因为最小高度已设置为
?android:attr/actionBarSize
。您可以检查它的行为

接下来,在扩展
AccountAuthenticatorActivity
LoginActivity上,您必须使用
AppCompatDelegate
,但在此之前,请确保LoginActivity实现
AppCompatCallback

public class LoginActivity extends AccountAuthenticatorActivity
        implements AppCompatCallback
{
    ...
    private AppCompatDelegate mAppCompatDelegate;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        ...
        mAppCompatDelegate = AppCompatDelegate.create(this, this);
        mAppCompatDelegate.setSupportActionBar(mToolbarLogin);

        ActionBar actionBar = mAppCompatDelegate.getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(getString(R.string.activity_login));
        mCollapsingToolbarLayoutLogin.setTitleEnabled(false);
    }
您必须实现三种方法,但根据我的研究,如果您只想显示
ActionBar
,它们似乎没有任何作用,因此在本用例中,您可以将它们留空:

@Override
public void onSupportActionModeStarted(ActionMode mode)
{

}

@Override
public void onSupportActionModeFinished(ActionMode mode)
{

}

@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback)
{
    return null;
}
mAppCompatDelegate=AppCompatDelegate.create(this,this)
在第一个参数上请求
活动
实例,第二个参数请求回调

还有一件事。如果希望返回按钮如代码所示出现,它不会响应单击。您可能必须覆盖它:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case android.R.id.home:
            onBackPressed();
            break;

        default:
            return super.onOptionsItemSelected(item);
    }

    return true;
}
我发现在使用它的过程中有一个小怪癖,就是在某些设备上状态栏显示为白色。我仍在寻找如何修复它,但至少
AccountAuthenticatorActivity
现在有一个
ActionBar
。我不喜欢这个解决方案,因为它可能有点老套。为
活动
实现一个看似基本的行为实在是太麻烦了

@Override
public void onSupportActionModeStarted(ActionMode mode)
{

}

@Override
public void onSupportActionModeFinished(ActionMode mode)
{

}

@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback)
{
    return null;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case android.R.id.home:
            onBackPressed();
            break;

        default:
            return super.onOptionsItemSelected(item);
    }

    return true;
}