gpt4 book ai didi

android - 对于方法 drawerLayout.openDrawer(GravityCompat.START),抽屉导航未从左侧打开

转载 作者:行者123 更新时间:2023-11-29 00:53:59 28 4
gpt4 key购买 nike

我的主屏幕.java

    public class HomeScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;

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

instantiateViews();
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);

}

public void instantiateViews(){

toolbar = findViewById(R.id.drawer_menu_toolbar);
drawerLayout = findViewById(R.id.home_screen_main_drawer_layout);
navigationView = findViewById(R.id.home_screen_navigation_view);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
Log.i("CLICKED","clicked");
drawerLayout.openDrawer(GravityCompat.START);
return true;
}


return super.onOptionsItemSelected(item);
}

我的问题:

首次单击 ActionBarToggle 时,抽屉导航未打开。它仅在我最初向右滑动时打开,之后,ActionBarToggle 和向右滑动都打开抽屉。只有当应用程序重新启动时,点击 ActionBarToggle 才不起作用。所以我尝试注册 ActionBarToggle 的点击并在日志中,每当我点击它时,它都会显示以下内容

Log.i

2019-06-21 10:00:11.860 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:13.282 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:14.201 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.031 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.830 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:16.383 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:17.920 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:18.497 7467-7467/com.example.gofresh I/CLICKED: clicked

我应该在 onOptionsItemSelected() 中放入什么代码,以便 ActionBarToggle 甚至响应第一次点击?

我的 home_screen.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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">

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

<!--Top Layout-->

<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="@color/blue">

<android.support.v7.widget.Toolbar
android:id="@+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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


<android.support.v7.widget.Toolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="@drawable/seachbar_homescreen"


<EditText
android:id="@+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />

</android.support.v7.widget.Toolbar>
</RelativeLayout>


</RelativeLayout>

<android.support.design.widget.NavigationView
android:id="@+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />


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

最佳答案

它认为您缺少可见性,因为您设置了导航 View 。android:visibility="gone"

<?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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">

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

<!--Top Layout-->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary">

<android.support.v7.widget.Toolbar
android:id="@+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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


<android.support.v7.widget.Toolbar
android:id="@+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="@android:"


<EditText
android:id="@+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />

</android.support.v7.widget.Toolbar>
</RelativeLayout>


</RelativeLayout>

<!--<android.support.design.widget.NavigationView
android:id="@+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />
-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/home_screen_menu_drawer_header"
app:menu="@menu/home_screen_menu_drawer" />

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

在切换问题之前处理代码运行时的可见性

关于android - 对于方法 drawerLayout.openDrawer(GravityCompat.START),抽屉导航未从左侧打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56696929/

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