如何在每个NavigationView菜单组中设置自己的样式

问题描述 投票:1回答:1

我有一个NavigationView,具有2种不同的元素样式。对于第一组,我在第二组中具有不同的字体,大小和文本颜色。

任何解决方案?

在我的主要活动中,我有我的NavigationView

   <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/activity_home_navigation_drawer"
        app:headerLayout="@layout/nav_header"/>

activity_home_navigaton抽屉

<?xml version="1.0" encoding="utf-8"?>

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

    <group android:checkableBehavior="single">

        <item android:id="@+id/nav_camera" android:icon="@drawable/ic_close_24px" android:title="opcion1"/>

        <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_close_24px" android:title="opcion2" />

        <item android:id="@+id/nav_manage" android:icon="@drawable/ic_close_24px" android:title="opcion3" />

        <item android:id="@+id/nav_share" android:icon="@drawable/ic_close_24px" android:title="opcion4" />

        <item android:id="@+id/nav_send" android:icon="@drawable/ic_close_24px" android:title="opcion5" />

    </group>

    <group android:checkableBehavior="single">

        <item android:id="@+id/otro" android:icon="@drawable/ic_close_24px" android:title="2opcion1" />

        <item android:id="@+id/pepe" android:icon="@drawable/ic_close_24px" android:title="2opcion2" />

        <item android:id="@+id/juan" android:icon="@drawable/ic_close_24px" android:title="2opcion3" />



    </group>

</menu>

enter image description here

java android menu navigationview
1个回答
0
投票

我回应自己。自定义导航视图:

在我的活动中:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent"
     android:layout_width="match_parent">

----other VIEWS

  <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
    />


</androidx.drawerlayout.widget.DrawerLayout>

创建菜单的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">



    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_menu_lateral_height"
        android:adjustViewBounds="false"
        android:scaleType="centerCrop"
        android:src="@drawable/menu_image_header" />

    <LinearLayout
        android:id="@+id/menu_lateral_header"
        android:paddingTop="67dp"
        android:paddingLeft="@dimen/header_menu_lateral_left_padding"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_menu_lateral_height">

        <TextView
            android:id="@+id/menu_lateral_hola"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/poppins_bold"
            android:textColor="@color/white"
            android:textSize="20dp"
            android:text="@string/menu_lateral_hola"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginTop="15dp"
            android:fontFamily="@font/muli_light"
            android:textColor="@color/white"
            android:text="@string/menu_lateral_editar_perfil"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/elementos_menu"
        android:paddingTop="36dp"
        android:paddingLeft="@dimen/header_menu_lateral_left_padding"
        app:layout_constraintTop_toBottomOf="@id/menu_lateral_header"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <LinearLayout
        android:id="@+id/elementos_footer_menu"
        android:paddingTop="36dp"
        android:paddingLeft="@dimen/header_menu_lateral_left_padding"
        app:layout_constraintTop_toBottomOf="@id/elementos_menu"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/txtv_menu_footer_app_version"
        android:paddingTop="30dp"
        android:paddingLeft="@dimen/header_menu_lateral_left_padding"
        android:fontFamily="@font/muli_light"
        android:textColor="@color/colorPrimary"
        android:textSize="12dp"
        android:text="Version de la app-...."
        app:layout_constraintTop_toBottomOf="@id/elementos_footer_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="40dp"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

为商品创建自定义组件

    public class LateralMenuItem extends LinearLayout {

    String text;
    int icon;

    public LateralMenuItem(Context context, String text, int icon) {
        super(context);
        this.text = text;
        this.icon = icon;
        init(context);
    }

    public LateralMenuItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LateralMenuItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LateralMenuItem(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void init(Context context){
        View rootView = inflate(context, R.layout.lateral_menu_item, this);
        TextView textView = rootView.findViewById(R.id.lateral_menu_item_tv);
        ImageView imageView = rootView.findViewById(R.id.menu_lateral_item_icon);
        textView.setText(text);
        imageView.setImageDrawable(context.getResources().getDrawable(icon));
    }
}


  private void addOptionsToMenu(LinearLayout menuBody){
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_inicio), R.drawable.menu_icon_heart));
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_lista_cursos), R.drawable.menu_icon_heart));
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_mis_cursos), R.drawable.menu_icon_heart));
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_training), R.drawable.menu_icon_heart));
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_trofeos), R.drawable.menu_icon_heart));
        menuBody.addView(new LateralMenuItem(this, getString(R.string.menu_lateral_opcion_estadisticas), R.drawable.menu_icon_estadistics));
    }
© www.soinside.com 2019 - 2024. All rights reserved.