当前位置:  开发笔记 > Android > 正文

Android-NavigationView从右到左

如何解决《Android-NavigationView从右到左》经验,为你挑选了2个好方法。

我正在使用最新版本的Android Studio(1.5),我想使用抽屉布局制作一个菜单,以便调用GravityCompat.

我正在尝试使用这些组件并对其进行修改,将Drawer从右向左放置.这是我的代码.

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;
}
}

XML









正如您所看到的,GravityCompat不允许我放右而不是END或START,如果我把它放在XML中,它会崩溃.

出现下一个错误:

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

Vassilis Pal.. 17

首先替换在DrawerLayout tools:openDrawer="start"tools:openDrawer="end".现在你的问题出在切换上,它打开了左抽屉,因为你只有正确的抽屉,它会抛出异常.您可以在操作栏(右侧)上添加自己的按钮以打开抽屉.要做到这一点改变你app_bar.xml这样




    

        

            

                

                    
                


        


    

    


然后像这样从您的Activity更改onCreate方法

@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);
}

您也可以在此处或此处找到抽屉的图像.我希望这能帮到您.:)



1> Vassilis Pal..:

首先替换在DrawerLayout tools:openDrawer="start"tools:openDrawer="end".现在你的问题出在切换上,它打开了左抽屉,因为你只有正确的抽屉,它会抛出异常.您可以在操作栏(右侧)上添加自己的按钮以打开抽屉.要做到这一点改变你app_bar.xml这样




    

        

            

                

                    
                


        


    

    


然后像这样从您的Activity更改onCreate方法

@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);
}

您也可以在此处或此处找到抽屉的图像.我希望这能帮到您.:)



2> 小智..:

像这样使用自定义的drawerLayout:

public class TaskManagerDrawerLayout  extends DrawerLayout {

public TaskManagerDrawerLayout(Context context) {
    super(context);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TaskManagerDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public void closeDrawer(int gravity) {
    super.closeDrawer(GravityCompat.END);
}

@Override
public void openDrawer(int gravity) {
    super.openDrawer(GravityCompat.END);
}

}

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有