gpt4 book ai didi

java - 操作栏抽屉切换不会从右侧打开导航

转载 作者:行者123 更新时间:2023-11-30 00:20:52 25 4
gpt4 key购买 nike

我对 NavigationView 和 ActionBarDrawer Toggle 有问题当我触摸 Toogle 时,应用程序关闭,这是错误:

    Process: net.diginic.master, PID: 21770
java.lang.IllegalArgumentException: No drawer view found with
gravity LEFT
at
android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1651)
at
android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1637)
at

android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:293)
at

android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:202)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

导航重力在右|端,重力在左没有问题但我想用 rtl 汉堡包菜单图标打开导航并从右边打开它

--MainActivity.java

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawerLayout;
NavigationView navigationView;
ActionBarDrawerToggle toggle;
Toolbar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

sliderLayout = (SliderLayout) findViewById(R.id.slider);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.layout_drawer);
navigationView = (NavigationView) findViewById(R.id.navigation_view);

setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);



}

@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
drawerLayout.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.drawer_home:
break;
case R.id.drawer_sort_list:
break;
case R.id.drawer_shopping_cart:
break;
case R.id.drawer_most_buy:
break;
case R.id.drawer_most_see:
break;
case R.id.drawer_newest:
break;
case R.id.drawer_settings:
break;
case R.id.drawer_faq:
break;
case R.id.drawer_about:
break;
}
drawerLayout.closeDrawer(Gravity.RIGHT);
return true;
}
}

--main_activity.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"
android:id="@+id/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end">

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

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layoutDirection="rtl"
app:headerLayout="@layout/header"
app:menu="@menu/main_drawer">

</android.support.design.widget.NavigationView>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

最佳答案

尝试将其添加到 onCreate() 的底部

toolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (Drawer.isDrawerOpen(Gravity.END)) {
Drawer.closeDrawer(Gravity.END);
} else {
Drawer.openDrawer(Gravity.END);
}
}
});

关于java - 操作栏抽屉切换不会从右侧打开导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com