AppCompat v7-r23。工具栏中的主页按钮默认不显示

时间:2015-09-19 10:40:10

标签: android navigation-drawer android-appcompat appcompat-v7-r23 android-navigation-drawer

我想打开MyActivity时默认显示Home(汉堡)按钮。不幸的是,我没有在左上角看到任何按钮。但是在我打开并关闭抽屉后会出现“主页”按钮。

我使用最新的appcompat-v7:23.0.1库:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}

我的活动:

public class MyActivity extends AppCompatActivity{

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mTitle;

    private Toolbar toolbar;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);

            getSupportActionBar().setHomeButtonEnabled(true);
        }

        mTitle = getTitle();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.hello_world,
                R.string.app_name)
        {
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                toolbar.setTitle(mTitle);
                invalidateOptionsMenu();
                syncState();
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //toolbar.setTitle(mDrawerTitle);
                invalidateOptionsMenu();
                syncState();
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }
}

activity_drawer.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    style="@style/MatchParent">

    <!-- Your normal content view -->
    <LinearLayout
        style="@style/MatchParent"
        android:orientation="vertical">

        <include layout="@layout/toolbar"/>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <FrameLayout
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start">

        <include layout="@layout/navigation_drawer" />

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

我已尝试以下变体默认显示“主页”按钮:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

但它也不起作用。

例如,如果我只设置:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

然后在我打开活动时正确显示向上(箭头)按钮,并在打开和关闭抽屉后转换为主页按钮。

可能是什么问题?

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您不是调用syncState onPostCreate ,或者调用onConfigurationChanged onOptionsItemSelected 对应于您的活动回调,您应该这样做。

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}