抽屉指示器隐藏用于顶级活动

时间:2016-01-07 20:51:43

标签: android android-drawer

我想显示抽屉的导航指示器,如下图所示:

enter image description here

由于某种原因,指示灯不显示。我花了很多时间在谷歌和SO上,没有解决方案适合我。

清单:

android:theme="@style/Theme.AppCompat.Light"

摇篮:

compile 'com.android.support:appcompat-v7:23.1.1'

布局(没有父母):

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:id="@+id/puzzleScreen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".activities.PuzzleActivity">

    <lelisoft.com.lelimath.view.TilesView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tiles"/>
</LinearLayout>

<ListView
    android:id="@+id/navList"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>

的活动:

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;

public class PuzzleActivity extends AppCompatActivity {

protected void onCreate(Bundle state) {
    super.onCreate(state);
    mDrawerList = (ListView)findViewById(R.id.navList);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();
    addDrawerItems();
    setupDrawer();

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Navigation!");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

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

    mDrawerToggle.syncState();
    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

private void addDrawerItems() {
    String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
    mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(PuzzleActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
        }
    });
}

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

问题出在哪里?我尝试使用Android 5的Nexus 4和使用Android 6的Nexus 5x。

更新:

我已经下载了Android导航抽屉样本,它在第二个活动中显示了抽屉。然后我将相关部分复制到主要活动 - 指标消失了。因此,似乎与我的活动没有父母有关。

1 个答案:

答案 0 :(得分:0)

在创建ActionBarDrawerToggle时,您似乎缺少图标。请参阅:http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html#ActionBarIcon

 mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
            ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
        }
    };