Java 我的导航抽屉所选项目未完全覆盖整个菜单区域

Java 我的导航抽屉所选项目未完全覆盖整个菜单区域,java,android,material-design,navigation-drawer,Java,Android,Material Design,Navigation Drawer,我正在使用材料导航抽屉制作导航抽屉。我已经应用了“onNavigationItemSelected”方法,并且正在选择菜单,但它只覆盖菜单的中间区域,并在顶部和底部留下区域。我希望选择的菜单覆盖整个区域,因为它应该。但我得到了以下结果 我试图改变所选项目的颜色,但迄今为止没有运气 以下是我所做工作的代码: XML布局: <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/

我正在使用材料导航抽屉制作导航抽屉。我已经应用了“onNavigationItemSelected”方法,并且正在选择菜单,但它只覆盖菜单的中间区域,并在顶部和底部留下区域。我希望选择的菜单覆盖整个区域,因为它应该。但我得到了以下结果

我试图改变所选项目的颜色,但迄今为止没有运气

以下是我所做工作的代码:

XML布局:

<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:context=".Ui.Support"
    android:fitsSystemWindows="true"
    android:id="@+id/consumer_drawer_layout"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:background="@color/dark"
        app:itemTextColor="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/consumer_navbar"
        app:menu="@menu/nav_menu_consumer"
        android:layout_gravity="start"
        app:itemBackground="@drawable/menu_selected"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/support_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/light"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ToolbarTheme"
                app:navigationIcon="@drawable/btn_menu_gray"
                app:title="SUPPORT"
                app:titleTextAppearance="@style/toolbar_title"
                app:titleTextColor="@color/dark">

                <ImageView
                    android:id="@+id/support_back_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:layout_marginEnd="10dp"
                    android:src="@drawable/btn_back_gray" />
            </androidx.appcompat.widget.Toolbar>

            <RelativeLayout
                android:layout_below="@+id/support_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/contact_us"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:padding="10dp"
                    android:text="@string/contact_us"
                    android:textColor="@color/dark"
                    android:textSize="16sp" />


                <com.google.android.material.card.MaterialCardView
                    android:id="@+id/security_card"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/contact_us"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="10dp"
                    app:cardCornerRadius="8dp"
                    app:cardElevation="10dp"
                    app:contentPaddingBottom="21dp"
                    app:contentPaddingTop="20dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                            <com.google.android.material.textview.MaterialTextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginStart="14dp"
                                android:layout_marginBottom="14dp"
                                android:drawableStart="@drawable/btn_phone"
                                android:drawablePadding="20dp"
                                android:text="@string/contact_us_no"
                                app:fontFamily="@font/avenirltstd_heavy" />
                        </LinearLayout>

                        <View
                            android:id="@+id/view_2"
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:background="@android:color/darker_gray" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                            <com.google.android.material.textview.MaterialTextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginStart="14dp"
                                android:layout_marginTop="14dp"
                                android:drawableStart="@drawable/btn_phone"
                                android:drawablePadding="20dp"
                                android:text="@string/www_welist_com"
                                app:fontFamily="@font/avenirltstd_heavy" />
                        </LinearLayout>

                        <View
                            android:id="@+id/view_3"
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:layout_marginTop="14dp"
                            android:background="@android:color/darker_gray" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">

                            <com.google.android.material.textview.MaterialTextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_marginStart="14dp"
                                android:layout_marginTop="14dp"
                                android:drawableStart="@drawable/btn_email"
                                android:drawablePadding="20dp"
                                android:text="@string/support_welist_com"
                                app:fontFamily="@font/avenirltstd_heavy" />
                        </LinearLayout>
                    </LinearLayout>
                </com.google.android.material.card.MaterialCardView>

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/social_folllow"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/security_card"
                    android:layout_marginStart="10dp"
                    android:padding="10dp"
                    android:text="@string/social_follow"
                    android:textColor="@color/dark"
                    android:textSize="16sp" />

                <LinearLayout
                    android:id="@+id/social_links"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/social_folllow"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="5">

                        <com.google.android.material.card.MaterialCardView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="15dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="10dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:foreground="?android:attr/selectableItemBackground"
                            app:cardCornerRadius="8dp"
                            app:cardElevation="10dp"
                            app:contentPaddingBottom="15dp"
                            app:contentPaddingLeft="5dp"
                            app:contentPaddingRight="5dp"
                            app:contentPaddingTop="15dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/social_whatsapp" />
                        </com.google.android.material.card.MaterialCardView>

                        <com.google.android.material.card.MaterialCardView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:foreground="?android:attr/selectableItemBackground"
                            app:cardCornerRadius="8dp"
                            app:cardElevation="10dp"
                            app:contentPaddingBottom="15dp"
                            app:contentPaddingLeft="5dp"
                            app:contentPaddingRight="5dp"
                            app:contentPaddingTop="15dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/social_facebook" />
                        </com.google.android.material.card.MaterialCardView>

                        <com.google.android.material.card.MaterialCardView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:foreground="?android:attr/selectableItemBackground"
                            app:cardCornerRadius="8dp"
                            app:cardElevation="10dp"
                            app:contentPaddingBottom="15dp"
                            app:contentPaddingLeft="5dp"
                            app:contentPaddingRight="5dp"
                            app:contentPaddingTop="15dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/social_twitter" />
                        </com.google.android.material.card.MaterialCardView>

                        <com.google.android.material.card.MaterialCardView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:foreground="?android:attr/selectableItemBackground"
                            app:cardCornerRadius="8dp"
                            app:cardElevation="10dp"
                            app:contentPaddingBottom="15dp"
                            app:contentPaddingLeft="5dp"
                            app:contentPaddingRight="5dp"
                            app:contentPaddingTop="15dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/social_linkedin" />
                        </com.google.android.material.card.MaterialCardView>

                        <com.google.android.material.card.MaterialCardView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            android:layout_marginEnd="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:foreground="?android:attr/selectableItemBackground"
                            app:cardCornerRadius="8dp"
                            app:cardElevation="10dp"
                            app:contentPaddingBottom="15dp"
                            app:contentPaddingLeft="5dp"
                            app:contentPaddingRight="5dp"
                            app:contentPaddingTop="15dp">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/social_youtube" />
                        </com.google.android.material.card.MaterialCardView>


                    </LinearLayout>

                    <com.google.android.material.textview.MaterialTextView
                        android:id="@+id/ask_us"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:padding="10dp"
                        android:text="@string/ask_us"
                        android:textColor="@color/dark"
                        android:textSize="16sp" />

                    <com.google.android.material.card.MaterialCardView
                        android:id="@+id/ask_us_card"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="10dp"
                        app:contentPaddingBottom="21dp"
                        app:contentPaddingTop="20dp">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">


                            <com.google.android.material.textfield.TextInputEditText
                                android:id="@+id/subject"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="10dp"
                                android:layout_marginEnd="10dp"
                                android:hint="@string/subject"
                                android:inputType="text" />

                            <com.google.android.material.textfield.TextInputEditText
                                android:id="@+id/support_message"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="10dp"
                                android:layout_marginEnd="10dp"
                                android:hint="@string/message"
                                android:inputType="text" />

                            <com.google.android.material.button.MaterialButton
                                android:id="@+id/ask_us_btn"
                                android:layout_width="match_parent"
                                android:layout_height="60dp"
                                android:layout_marginStart="15dp"
                                android:layout_marginTop="15dp"
                                android:layout_marginEnd="15dp"
                                android:text="@string/submit"
                                app:backgroundTint="@color/main"
                                app:cornerRadius="8dp" />
                        </LinearLayout>

                    </com.google.android.material.card.MaterialCardView>
                </LinearLayout>

            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
public class Support extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar support_toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_support);
        drawerLayout = findViewById(R.id.consumer_drawer_layout);
        navigationView = findViewById(R.id.consumer_navbar);
        support_toolbar = findViewById(R.id.support_toolbar);
        setSupportActionBar(support_toolbar);
        navigationView.setItemIconTintList(null);

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, support_toolbar, R.string.nav_open, R.string.nav_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return true;
    }
}

不要为每个项目创建组,而是将所有项目合并到一个组中,如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group
        android:id="@+id/group_item_1"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/menu_home"
            android:title="@string/home" />

        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/menu_profile"
            android:title="@string/profile" />

        <item
            android:id="@+id/nav_categories"
            android:icon="@drawable/menu_categories"
            android:title="@string/all_categories" />

        <item
            android:id="@+id/nav_nearby_brands"
            android:icon="@drawable/menu_nearby"
            android:title="@string/brand_nearby" />

        <item
            android:id="@+id/nav_professionals"
            android:icon="@drawable/menu_professional"
            android:title="@string/professionals" />

        <item
            android:id="@+id/nav_vendors"
            android:icon="@drawable/menu_vendor"
            android:title="@string/vendors" />
    </group>
</menu>
navigationView = findViewById(R.id.consumer_navbar);
NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(Support.this,DividerItemDecoration.VERTICAL));

当您为每个项目创建多个组时,这是预期的。尝试将所有项目合并到单个项目中group@Md.Asaduzzaman那么我将如何实现菜单分隔符,你能指导我,如果他们是另一种方式。谢谢您是否可以添加所选的
菜单?
navigationView = findViewById(R.id.consumer_navbar);
NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(Support.this,DividerItemDecoration.VERTICAL));