导航抽屉切换图标不可见



我使用以下代码设置导航抽屉,但导航抽屉的图标不可见。

public class NavigationDrawer extends ActionBarActivity {
private int mPosition = -1;
private String mTitle = "";
private String[] mDashBoardList;
private int[] mIcons = new int[]{R.drawable.dashboard,
        R.drawable.my_orders, R.drawable.testimonials,
        R.drawable.notifications, R.drawable.contact_us, R.drawable.sync,
        R.drawable.logout};
private String[] mCount = new String[]{"", "", "", "", "", "", "",};
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@SuppressWarnings("deprecation")
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout mDrawer;
private List<HashMap<String, String>> mList;
private SimpleAdapter mAdapter;
final private String DRAWER_CONTENT = "vaighai";
final private String FLAG = "flag";
final private String COUNT = "count";
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_drawer_main);
    mDashBoardList = getResources().getStringArray(R.array.drawerContents);
    mTitle = (String) getTitle();
    mDrawerList = (ListView) findViewById(R.id.drawer_lists);
    mDrawer = (LinearLayout) findViewById(R.id.drawer);
    mList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 7; i++) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put(DRAWER_CONTENT, mDashBoardList[i]);
        hm.put(COUNT, mCount[i]);
        hm.put(FLAG, Integer.toString(mIcons[i]));
        mList.add(hm);
    }
    String[] from = {FLAG, DRAWER_CONTENT, COUNT};
    int[] to = {R.id.flag, R.id.country, R.id.count};
    mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from,
            to);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            highlightSelectedItem();
            supportInvalidateOptionsMenu();
        }
        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle("");
            supportInvalidateOptionsMenu();
        }
    };

    if (mDrawer != null)
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {
            // incrementHitCount(position);
            if (position < 5) {
                showFragment(position);
            } else {
                Toast.makeText(getApplicationContext(),
                        mDashBoardList[position], Toast.LENGTH_LONG).show();
            }
            mDrawerLayout.closeDrawer(mDrawer);
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#2cc3b7"));
    getSupportActionBar().setBackgroundDrawable(colorDrawable);
    mDrawerList.setAdapter(mAdapter);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void incrementHitCount(int position) {
    HashMap<String, String> item = mList.get(position);
    String count = item.get(COUNT);
    item.remove(COUNT);
    if (count.equals("")) {
        count = "  1  ";
    } else {
        int cnt = Integer.parseInt(count.trim());
        cnt++;
        count = "  " + cnt + "  ";
    }
    item.put(COUNT, count);
    mAdapter.notifyDataSetChanged();
}
public void showFragment(int position) {
    mTitle = mDashBoardList[position];
    DrawerFragment cFragment = new DrawerFragment();
    Bundle data = new Bundle();
    data.putInt("position", position);
    cFragment.setArguments(data);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.content_frame, cFragment);
    ft.commit();
}
public void highlightSelectedItem() {
    int selectedItem = mDrawerList.getCheckedItemPosition();
    if (selectedItem > 4)
        mDrawerList.setItemChecked(mPosition, true);
    else
        mPosition = selectedItem;
    if (mPosition != -1)
        getSupportActionBar().setTitle(mDashBoardList[mPosition]);
}
@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

}

我已经使用了mDrawerToggle.syncState(),但问题仍然存在。我也尝试过设置 getSupportActionBar().setDisplayShowHomeEnabled(true);getSupportActionBar().setDisplayShowHomeEnabled(false); 但不是工作。

添加此行:

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

当然,在工具栏的 id 上检查您的 xml 文件,默认值应该是工具栏。

初始化

后写mDrawerToggle mDrawerToggle.syncState();

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, R.string.drawer_open,
        R.string.drawer_close) {
    public void onDrawerClosed(View view) {
        highlightSelectedItem();
        supportInvalidateOptionsMenu();
    }
    public void onDrawerOpened(View drawerView) {
        getSupportActionBar().setTitle("");
        supportInvalidateOptionsMenu();
    }
};
mDrawerToggle.syncState();

通常每个人都在使用工具栏,但因为你不在ActionBarActivity。我想提一下,ActionBar已被弃用。

您可能想尝试使用Android Studio提供的默认NavigationActivity

相关内容

  • 没有找到相关文章