android/java:如何在bottomnavigationview(初始化时没有颜色选择)中以编程方式更改可绘图的颜色?

ac1kyiln  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(396)

我的xml中默认有一种颜色,我想通过编程来更改它。我不知道如何以编程方式访问highlight\u color.xml中的highlight\u color项颜色。。。
主要java代码:

  1. //BEGIN TEST COLOR
  2. ColorStateList iconsColorStates = new ColorStateList(
  3. new int[][]{
  4. new int[]{-android.R.attr.state_checked},
  5. new int[]{android.R.attr.state_checked}
  6. },
  7. new int[]{
  8. Color.parseColor("#000000"),
  9. Color.parseColor("#ffffff")
  10. });
  11. ColorStateList textColorStates = new ColorStateList(
  12. new int[][]{
  13. new int[]{-android.R.attr.state_checked},
  14. new int[]{android.R.attr.state_checked}
  15. },
  16. new int[]{
  17. Color.parseColor("#000000"),
  18. Color.parseColor("#ffffff")
  19. });
  20. //color ec5d60
  21. bottomNavigation.setItemIconTintList(iconsColorStates);
  22. bottomNavigation.setItemTextColor(textColorStates);

主xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/relativeLayout3"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".LocalNav">
  9. <WebView
  10. android:id="@+id/vieweb"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_marginStart="0dp"
  14. android:layout_marginBottom="-2dp"
  15. app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <com.NewTelApps.ToEvent.newBottomNav
  19. android:id="@+id/bottom_nav"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:background="#ffffff"
  23. app:itemIconSize="45dp"
  24. android:visible="false"
  25. app:itemBackground="@drawable/nav_item_drawable"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintStart_toStartOf="parent"
  28. app:menu="@menu/bottom_nav_menu"
  29. />
  30. </androidx.constraintlayout.widget.ConstraintLayout>

nav\u item\u drawable.xml文件

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

突出显示\u color.xml

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

编辑:
这两段代码(取自不同的答案)颜色变化很好,但显示不好:
1/

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

2/

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

在添加此代码之前:

添加此代码后:

编辑2:
我终于找到了解决问题的方法。我只需在颜色项tint和文本之前将代码片段放入一个循环中。这是我的完整代码:
主java类

  1. int itemSelected = this.getSelectedItem(this);
  2. Log.d("Item Selected :", String.valueOf(itemSelected));
  3. Log.d("Number of items :", String.valueOf(this.getMenu().size()));
  4. if (initialization == 0)
  5. {
  6. this.getMenu().setGroupCheckable(0, true, false);
  7. for (int j = 0; j < this.getMenu().size(); j++) {
  8. this.getMenu().getItem(j).setChecked(false);
  9. }
  10. this.getMenu().setGroupCheckable(0, true, true);
  11. }
  12. final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
  13. final int bottomMenuChildCount = bottomMenu.getChildCount();
  14. BottomNavigationItemView item = null;
  15. View itemTitle;
  16. for(int i=0; i<bottomMenuChildCount; i++){
  17. item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
  18. //this shows all titles of items
  19. item.setChecked(true);
  20. //every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle
  21. itemTitle = item.getChildAt(1);
  22. //every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
  23. ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTextSize(10);
  24. ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setGravity(Gravity.CENTER_HORIZONTAL);
  25. ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTextSize(10);
  26. ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setGravity(Gravity.CENTER_HORIZONTAL);
  27. Drawable drawable = AppCompatResources.getDrawable(context,
  28. R.drawable.nav_item_drawable);
  29. drawable.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.SRC_ATOP);
  30. item.setItemBackground(drawable);
  31. }
  32. //BEGIN TEST COLOR
  33. ColorStateList iconsColorStates = new ColorStateList(
  34. new int[][]{
  35. new int[]{-android.R.attr.state_checked},
  36. new int[]{android.R.attr.state_checked}
  37. },
  38. new int[]{
  39. Color.parseColor("#000000"),
  40. Color.parseColor("#ffffff")
  41. });
  42. ColorStateList textColorStates = new ColorStateList(
  43. new int[][]{
  44. new int[]{-android.R.attr.state_checked},
  45. new int[]{android.R.attr.state_checked}
  46. },
  47. new int[]{
  48. Color.parseColor("#000000"),
  49. Color.parseColor("#ffffff")
  50. });
  51. //color ec5d60
  52. bottomNavigation.setItemIconTintList(iconsColorStates);
  53. bottomNavigation.setItemTextColor(textColorStates);

提前谢谢。

ny6fqffe

ny6fqffe1#

你可以试试这个:)

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

omvjsjqw2#

可以通过编程方式更改背景色,如下所示:

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

并保持 app:itemBackground="@drawable/nav_item_drawable" 你的 BottomNavigationView 希望它能瞄准它!

相关问题