【问题标题】:How to set their own style at each NavigationView menu group如何在每个 NavigationView 菜单组设置自己的样式
【发布时间】:2020-05-17 16:59:43
【问题描述】:

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

有什么办法吗?

在我的主要活动中,我有我的导航视图

   <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>

【问题讨论】:

    标签: java android menu navigationview


    【解决方案1】:

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

    在我的活动中:

    <?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));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2023-03-17
      • 2017-10-24
      • 1970-01-01
      • 2021-01-05
      • 2013-10-28
      • 2012-08-31
      • 2015-09-14
      相关资源
      最近更新 更多