Android/Java:如何以编程方式更改 BottomNavigationView 中 Drawable 的颜色(初始化时没有选择颜色)?

Android/Java: How to change programmatically the color of a Drawable in a BottomNavigationView (with no color selection at initialization)?

我的 XML 中默认有一种颜色,我想以编程方式更改它。我不知道如何以编程方式访问 highlight_color.xml 中的 highlight_color 项目颜色...

主要java代码:

        //BEGIN TEST COLOR
        ColorStateList iconsColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#000000"),
                        Color.parseColor("#ffffff")
                });

        ColorStateList textColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#000000"),
                        Color.parseColor("#ffffff")
                });
        //color ec5d60
        bottomNavigation.setItemIconTintList(iconsColorStates);
        bottomNavigation.setItemTextColor(textColorStates);

主要XML:

   <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/relativeLayout3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".LocalNav">
    
        <WebView
            android:id="@+id/vieweb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="0dp"
            android:layout_marginBottom="-2dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.NewTelApps.ToEvent.newBottomNav
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            app:itemIconSize="45dp"
            android:visible="false"
            app:itemBackground="@drawable/nav_item_drawable"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_nav_menu"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>

nav_item_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/highlight_color" android:state_checked="true"/>
</selector>

highlight_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ec5d60"/>
</shape>

编辑:

这两段代码(取自不同的答案)很好地改变了颜色,但显示效果不好:

1/

Drawable drawable = AppCompatResources.getDrawable(context,
        R.drawable.nav_item_drawable);
drawable.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.MULTIPLY);
bottomNavigation.setItemBackground(drawable);

2/

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context,
        R.drawable.nav_item_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.GREEN);

Before adding this piece of code:

After adding this piece of code:

编辑 2:

我终于找到解决问题的方法了。我只需要将这段代码放在颜色项目色调和文本之前的循环中。这是我的完整代码:

主要Javaclass

 int itemSelected = this.getSelectedItem(this);
    Log.d("Item Selected :", String.valueOf(itemSelected));

    Log.d("Number of items :", String.valueOf(this.getMenu().size()));

    if (initialization == 0)
    {
        this.getMenu().setGroupCheckable(0, true, false);
        for (int j = 0; j < this.getMenu().size(); j++) {
                this.getMenu().getItem(j).setChecked(false);
        }
        this.getMenu().setGroupCheckable(0, true, true);
    }

    final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
    final int bottomMenuChildCount = bottomMenu.getChildCount();
    BottomNavigationItemView item = null;



    View itemTitle;
    for(int i=0; i<bottomMenuChildCount; i++){
        item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
        //this shows all titles of items
        item.setChecked(true);

        //every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle

        itemTitle = item.getChildAt(1);
        //every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
        ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTextSize(10);
        ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setGravity(Gravity.CENTER_HORIZONTAL);
        ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTextSize(10);
        ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setGravity(Gravity.CENTER_HORIZONTAL);

        Drawable drawable = AppCompatResources.getDrawable(context,
                R.drawable.nav_item_drawable);
        drawable.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.SRC_ATOP);
        item.setItemBackground(drawable);
    }
        //BEGIN TEST COLOR
        ColorStateList iconsColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#000000"),
                        Color.parseColor("#ffffff")
                });

        ColorStateList textColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#000000"),
                        Color.parseColor("#ffffff")
                });
        //color ec5d60
        bottomNavigation.setItemIconTintList(iconsColorStates);
        bottomNavigation.setItemTextColor(textColorStates); 

提前致谢。

你可以试试这个:)

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, 
R.drawable.my_drawable); 
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);    

您可以通过编程方式更改背景颜色:

Drawable drawable = AppCompatResources.getDrawable(context,
        R.drawable.nav_item_drawable);
drawable.setColorFilter(getResources().getColor(android.R.color.holo_red_dark), PorterDuff.Mode.MULTIPLY);
bottomNavigationView.setItemBackground(drawable);

并保留 BottomNavigationView

app:itemBackground="@drawable/nav_item_drawable"

希望它有针对性!