始终收到错误消息:找不到左侧的抽屉视图

huangapple go评论72阅读模式
英文:

Always getting the error messsage: No drawer view found with gravity LEFT

问题

我正在开发一个应用程序,但在抽屉布局方面遇到了一些问题。我总是收到类似标题中提到的错误消息,但我没有LEFT值的Gravity。我只有END值的Gravity,就是这样。

这是我的Java代码:

public class HomeActivity extends AppCompatActivity {

    private ActionBarDrawerToggle toggle;

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

        Toolbar mainToolBar = findViewById(R.id.main_page_toolbar);
        DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
        NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);

        setSupportActionBar(mainToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(true);

        toggle = new ActionBarDrawerToggle(this, mainDrawerLayout, mainToolBar, R.string.openDrawer, R.string.closeDrawer);
        mainDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        toggle.onConfigurationChanged(newConfig);
    }
}

这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="end"
    tools:context=".Activities.HomeActivity"
    android:id="@+id/main_page_drawer_layout"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        app:headerLayout="@layout/home_side_menu_header_layout"
        app:menu="@menu/home_side_menu"
        android:layoutDirection="rtl"
        android:id="@+id/main_page_navigation_view"/>

</androidx.drawerlayout.widget.DrawerLayout>
英文:

I'm working on an app but I have some troubles with drawer layout. I always getting the error message like I mentioned in the title but I have no such Gravity with LEFT value. All I have are gravities with END values and that's all.
Here is my Java code:

public class HomeActivity extends AppCompatActivity {

private ActionBarDrawerToggle toggle;

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

    Toolbar mainToolBar =findViewById(R.id.main_page_toolbar);
    DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
    NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);

    setSupportActionBar(mainToolBar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);


    toggle = new ActionBarDrawerToggle(this,mainDrawerLayout,mainToolBar,R.string.openDrawer,R.string.closeDrawer);
    mainDrawerLayout.addDrawerListener(toggle);
    toggle.syncState();

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    toggle.onConfigurationChanged(newConfig);
}
}

And here is my XML code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout
 xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
 xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
 xmlns:tools=&quot;http://schemas.android.com/tools&quot;
 android:layout_width=&quot;match_parent&quot;
 android:layout_height=&quot;match_parent&quot;
 tools:openDrawer=&quot;end&quot;
 tools:context=&quot;.Activities.HomeActivity&quot;
 android:id=&quot;@+id/main_page_drawer_layout&quot;
 android:fitsSystemWindows=&quot;true&quot;&gt;

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;include layout=&quot;@layout/home_main_page&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_width=&quot;match_parent&quot;/&gt;

&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;


&lt;com.google.android.material.navigation.NavigationView
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_gravity=&quot;end&quot;
    app:headerLayout=&quot;@layout/home_side_menu_header_layout&quot;
    app:menu=&quot;@menu/home_side_menu&quot;
    android:layoutDirection=&quot;rtl&quot;
    android:id=&quot;@+id/main_page_navigation_view&quot;/&gt;

</androidx.drawerlayout.widget.DrawerLayout>

答案1

得分: 1

你需要处理工具栏上的“导航”点击,如下所示:

mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
            mainDrawerLayout.closeDrawer(GravityCompat.END);
        else
            mainDrawerLayout.openDrawer(GravityCompat.END);
    }
});

还要记得在需要时关闭抽屉,如下所示:

mainDrawerLayout.closeDrawer(GravityCompat.END)
英文:

You need to handle navigation click on Toolbar like below:

mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
            mainDrawerLayout.closeDrawer(GravityCompat.END);
        else
            mainDrawerLayout.openDrawer(GravityCompat.END);
    }
});

Also don't forgot to close drawer whenever needed like below:

mainDrawerLayout.closeDrawer(GravityCompat.END)

huangapple
  • 本文由 发表于 2020年1月4日 01:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定