Я создаю навигационный ящик, используя Material Navigation Drawer. Я применил метод "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>
Я слежу за онлайн-учебником, но их результат отличается от моего. Я также использую пункты меню в группах из-за достижения разделителей между пунктами.
Вот макет меню:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<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" />
</group>
<group
android:id="@+id/group_item_2"
android:checkableBehavior="single">
<item
android:id="@+id/nav_profile"
android:icon="@drawable/menu_profile"
android:title="@string/profile" />
</group>
<group
android:id="@+id/group_item_3"
android:checkableBehavior="single">
<item
android:id="@+id/nav_categories"
android:icon="@drawable/menu_categories"
android:title="@string/all_categories" />
</group>
<group
android:id="@+id/group_item_4"
android:checkableBehavior="single">
<item
android:id="@+id/nav_nearby_brands"
android:icon="@drawable/menu_nearby"
android:title="@string/brand_nearby" />
</group>
<group
android:id="@+id/group_item_5"
android:checkableBehavior="single">
<item
android:id="@+id/nav_professionals"
android:icon="@drawable/menu_professional"
android:title="@string/professionals" />
</group>
<group
android:id="@+id/group_item_6"
android:checkableBehavior="single">
<item
android:id="@+id/nav_vendors"
android:icon="@drawable/menu_vendor"
android:title="@string/vendors" />
</group>
</menu>
Вот мой Java Класс:
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;
}
}