将导航抽屉设置为右边说错误没有找到带有重力的抽屉视图左

时间:2017-01-23 06:26:26

标签: android android-drawable

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:f="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fffba9"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/container_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include
                    android:id="@+id/toolbar"
                    layout="@layout/toolbar" />
            </LinearLayout>

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </FrameLayout>

        </LinearLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#FFFFFF"
            android:choiceMode="singleChoice"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dp" />

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

</RelativeLayout>

代码

mTitle = mDrawerTitle = getTitle();
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

setupToolbar();

DataModel[] drawerItem = new DataModel[1];

drawerItem[0] = new DataModel(R.drawable.connect, "exit");

getSupportActionBar().setHomeButtonEnabled(true);

DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
setupDrawerToggle();

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

}

private void selectItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new ConnectFragment();
            break;
        case 1:
            fragment = new FixturesFragment();
            break;
        case 2:
            //fragment = new TableFragment();
            session.clear();
            Intent i = new Intent(Products.this, MainActivity.class);
            startActivity(i);
            finish();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

void setupToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}

void setupDrawerToggle() {
    mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.empty, R.string.empty);
    //This is necessary to change the icon of the Drawer Toggle upon state change.
    mDrawerToggle.syncState();
}

现在,当我点击图标ic_drwable显示菜单说错误No drawer view found with gravity LEFT

如何将导航抽屉设置在屏幕右侧。

我设置android:layout_gravity="end"所有内容都可以,但是菜单位于屏幕左侧。

我想在屏幕左侧显示抽屉和

我想将移动切换按钮改为右侧

0 个答案:

没有答案