Android的NavigationView从右到左 [英] Android-NavigationView from right to left

查看:391
本文介绍了Android的NavigationView从右到左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android Studio中的最后一个版本(1.5),我想用抽屉布局,进行位置其呼叫GravityCompat使菜单。

我想利用这个组件并对其进行修改,把抽屉从右到左的。这里是我的code。

 公共类MainActivity扩展AppCompatActivity
    实现NavigationView.OnNavigationItemSelectedListener {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    工具条工具栏=(栏)findViewById(R.id.toolbar);
    setSupportActionBar(工具栏);    DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle切换=新ActionBarDrawerToggle(
            对此,抽屉,工具栏,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.setDrawerListener(切换);
    toggle.syncState();    NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(本);
}@覆盖
公共无效onBack pressed(){
    DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
    如果(drawer.isDrawerOpen(GravityCompat.END)){
        drawer.closeDrawer(GravityCompat.END);
    }其他{
        super.onBack pressed();
    }
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}@燮pressWarnings(StatementWithEmptyBody)
@覆盖
公共布尔onNavigationItemSelected(菜单项项){
    //处理导航视图项点击这里。
    INT ID = item.getItemId();    如果(ID == R.id.nav_camera){
        //处理相机行动
    }否则如果(ID == R.id.nav_gallery){    }否则如果(ID == R.id.nav_slideshow){    }否则如果(ID == R.id.nav_manage){    }    DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.END);
    返回true;
}
}

中的XML

 <?XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.widget.DrawerLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:openDrawer =开始><包括
    布局=@布局/ app_bar_main
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>< android.support.design.widget.NavigationView
    机器人:ID =@ + ID / nav_view
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =结束
    机器人:fitsSystemWindows =真
    应用:菜单=@菜单/ activity_main_drawer/>< /android.support.v4.widget.DrawerLayout>

正如你所看到的,GravityCompat不要让我把权,而不是结束或开始,如果我把它放在XML,它崩溃。

下一个错误:

  java.lang.IllegalArgumentException异常:没有重力LEFT发现抽屉查看


解决方案

在DrawerLayout全部替换工具第一:以 >工具:openDrawer =结束。现在,你的问题是在切换,打开左边抽屉,因为你只有正确的抽屉里就抛出异常。您可以添加在动作条(右侧),您自己的按钮打开抽屉。要做到这一点改变你的 app_bar.xml 的这样

 <?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    工具:上下文=org.vspallas.myapplication.MainActivity>    < android.support.design.widget.AppBarLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:主题=@风格/ AppTheme.AppBarOverlay>        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>            < android.support.v7.widget.Toolbar
                机器人:ID =@ + ID /工具栏
                机器人:layout_width =match_parent
                机器人:layout_height =?ATTR / actionBarSize
                机器人:ATTR / colorPrimary背景=
                应用:popupTheme =@风格/ AppTheme.PopupOverlay/>                <的FrameLayout
                    机器人:ID =@ + ID / drawer_button
                    机器人:layout_width =50dp
                    机器人:layout_height =?ATTR / actionBarSize
                    机器人:layout_alignParentRight =真
                    机器人:可点击=真正的>                    < ImageView的
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:layout_gravity =CENTER_HORIZONTAL | center_vertical
                        机器人:SRC =@的mipmap / ic_drawer/>
                < /&的FrameLayout GT;
        < / RelativeLayout的>
    < /android.support.design.widget.AppBarLayout>    <包括布局=@布局/ content_main/>< /android.support.design.widget.CoordinatorLayout>

然后从你的活动这样的改变你onCreate方法

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    工具条工具栏=(栏)findViewById(R.id.toolbar);
    setSupportActionBar(工具栏);    findViewById(R.id.drawer_button).setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            //打开抽屉权
            DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
            drawer.openDrawer(GravityCompat.END);
        }
    });    NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(本);
}

您也可以在这里找到为你的抽屉的图像或的此处
我希望这会帮助你。 :)

I'm using The last version of Android Studio (1.5) and I want to make a menu using Drawer Layout, for position its call GravityCompat.

I'm trying to use this components and modify it, putting the Drawer out from right to left. Here is my code.

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

    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 = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.END);
    return true;
}
}

The XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout   
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<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="wrap_content"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />

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

As you can see, GravityCompat don't let me to put right instead of END or START, and if I put it in the XML, it crash.

with the next error:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

解决方案

First of all replace on DrawerLayout the tools:openDrawer="start" with tools:openDrawer="end". Now your problem is on the toggle, which opens the left drawer, and because you have only right drawer it throws the exception. You can add your own button on the actionbar (on the right side) to open the drawer. To do that change your app_bar.xml like this

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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:fitsSystemWindows="true"
    tools:context="org.vspallas.myapplication.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

                <FrameLayout
                    android:id="@+id/drawer_button"
                    android:layout_width="50dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_alignParentRight="true"
                    android:clickable="true">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:src="@mipmap/ic_drawer" />
                </FrameLayout>


        </RelativeLayout>


    </android.support.design.widget.AppBarLayout>

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

</android.support.design.widget.CoordinatorLayout>

Then change your onCreate method from your Activity like this

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

    findViewById(R.id.drawer_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // open right drawer
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.openDrawer(GravityCompat.END);
        }
    });

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

Also you can find the image for your drawer here or here. I hope this will help you. :)

这篇关于Android的NavigationView从右到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆