导航视图菜单项单击背景UI问题

时间:2016-06-20 11:08:52

标签: android background navigation-drawer android-navigationview

我一直在尝试使用抽屉布局来实现导航视图。它在API 21以上的手机中工作得非常好。但是在API 21下运行android的设备中它会失真。请参阅附带的屏幕截图。See how the item gets distorted background. < / p>

另外,我已经为hdpi,mdpi,xhdpi,xxhdpi和xxxhdpi密码集成了所需的图像。对于API 21及更高版本,我创建了一个drawable-v21目录,并为菜单项添加了Vector assests。可以请某人帮助我有这个问题吗?

以下是实现navigationView的MainActivity。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    titleText = (TextView) toolbar.findViewById(R.id.titleText);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame, Fragment1.newInstance("Fragment1"));
    fragmentTransaction.commit();

    navigationView.getMenu().getItem(0).setChecked(true);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (id == R.id.nav_home) {
        fragmentTransaction.replace(R.id.frame, Fragment1.newInstance("Fragment1"));
        fragmentTransaction.commit();
        titleText.setText("Frag1");
    } else if (id == R.id.nav_violations) {
        fragmentTransaction.replace(R.id.frame, Fragment2.newInstance("Fragment2"));
        fragmentTransaction.commit();
        titleText.setText("Frag2");
    } else if (id == R.id.nav_rem_drive_time) {
        fragmentTransaction.replace(R.id.frame, Fragment3.newInstance("Fragment3"));
        fragmentTransaction.commit();
        titleText.setText("Frag3");

    } else if (id == R.id.nav_downloads) {
        fragmentTransaction.replace(R.id.frame, Fragment4.newInstance("Fragment4"));
        fragmentTransaction.commit();
        titleText.setText("Frag4");

    } else if (id == R.id.nav_logout) {
        Toast.makeText(MainActivity.this, "Logout clicked", Toast.LENGTH_LONG).show();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

以下是xml代码:

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<!--app:itemBackground="@drawable/drawer_item_selector"
app:itemIconTint="@drawable/drawer_item"
app:itemTextColor="@drawable/drawer_item"-->

0 个答案:

没有答案