我的xml中默认有一种颜色,我想通过编程来更改它。我不知道如何以编程方式访问highlight\u color.xml中的highlight\u 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\u item\u 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>
突出显示\u 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);
在添加此代码之前:
添加此代码后:
编辑2:
我终于找到了解决问题的方法。我只需在颜色项tint和文本之前将代码片段放入一个循环中。这是我的完整代码:
主java类
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);
提前谢谢。
2条答案
按热度按时间ny6fqffe1#
你可以试试这个:)
omvjsjqw2#
可以通过编程方式更改背景色,如下所示:
并保持
app:itemBackground="@drawable/nav_item_drawable"
你的BottomNavigationView
希望它能瞄准它!