如何通过两个主题更改BottomNavBar的活动图标颜色?

时间:2018-07-11 10:59:00

标签: android android-layout android-studio bottomnavigationview

我试图通过创建自定义样式并在MainActivity中使用setTheme()方法在我的应用上实现浅色和深色方案。它工作正常,除了在黑暗模式下底部导航栏仅有一个问题之外。我要求活动图标为白色。这是当前的用户界面:黑暗模式,活动图标为黑色:

enter image description here

我尝试使用选择器文件重写并以自定义样式引用它,但是它不起作用。如何覆盖颜色?

这就是我希望在我的应用中实现的功能。 带有白色活动图标的底部导航栏:

enter image description here

编辑:这与Selected tab's color in Bottom Navigation View不同,因为在我的案例中有两个不同的主题。遵循给出的答案会影响我的灯光模式主题,在这里我根本看不到图标。

编辑2:我将与主题相关的代码包括如下:

MainActivity.java中的onCreate函数

    SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    String use_theme = preferences.getString("THEME", "");

    if(use_theme.equals("light_theme")) {
        setTheme(R.style.AppTheme);
    }

    else if(use_theme.equals("dark_theme")) {
        setTheme(R.style.DarkTheme);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextMessage = findViewById(R.id.message);
    final BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    Button lightButton = (Button) findViewById(R.id.light_button);
    lightButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
            editor.putString("THEME", "light_theme");
            editor.apply();

            Intent intent = getIntent();
            finish();

            startActivity(intent);
        }
    });

    Button darkButton = (Button) findViewById(R.id.dark_button);
    darkButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
            editor.putString("THEME", "dark_theme");
            editor.apply();

            Intent intent = getIntent();
            finish();

            startActivity(intent);
        }
    });

styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="DarkTheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/darkColorPrimary</item>
        <item name="colorPrimaryDark">@color/darkColorPrimaryDark</item>
        <item name="colorAccent">@color/colorWhite</item>
        <item name="itemBackground">@drawable/selector</item>
    </style>

selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@android:color/white" />
        <item android:color="@android:color/darker_gray"  />
    </selector>

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/light_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Light" />

        <Button
            android:id="@+id/dark_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Dark" />

    </LinearLayout>

    <Button
        android:id="@+id/normal_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/borderless_button"
        android:text="normal"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="116dp" />

    <Button
        android:id="@+id/borderless_button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/colored_button"
        android:text="borderless"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="164dp" />

    <Button
        android:id="@+id/colored_button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="colored"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="219dp" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radioButton"
        android:text="New CheckBox"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="267dp" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/switchButton"
        android:text="New RadioButton"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="299dp" />

    <Switch
        android:id="@+id/switchButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar"
        android:text="New Switch"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="331dp" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="358dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText"
        android:text="New Text"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="376dp" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="61dp"
        android:hint="Input"
        android:inputType="text"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="395dp" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:text="Home" />

</RelativeLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        app:itemIconTint="@drawable/selector" <!--Line which causes issue in light mode, initially excluded-->
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

    </android.support.constraint.ConstraintLayout>

Image with light mode on using itemIconTint in layout file

0 个答案:

没有答案