如何使用波纹修复按下效果 BottomNavigation 变得不可见 - AndroidX

问题描述 投票:0回答:2

我使用background=ripple在BottomNavigation中制作按压效果,但在我的例子中,MyBottomNavigation颜色很暗,因此由于波纹效果颜色是灰色,它似乎变得不可见,有其他方法可以解决这个问题吗?

//MainActivity.java

//BottomNavigation
    BottomNavigationView mBottomNav = findViewById(R.id.Bottom_Navigation);

    mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            Fragment selectedFragment = null;

            switch(menuItem.getItemId()){
                case R.id.Nav_Home:
                    selectedFragment = new HomePageFragment();
                    break;

                /*case R.id.Nav_Favorites:
                selectedFragment = new FavoritesPageFragment();
                    break;*/

                /*case R.id.Nav_Favorites:
                    selectedFragment = new SettingPageFragment();
                    break;*/

                case R.id.Nav_Follow:
                    selectedFragment = new FollowPageFragment();
                    break;

                /*case R.id.Nav_Chat:
                    selectedFragment = new ChatPageFragment();
                    break;*/

                case R.id.Nav_Profile:
                    selectedFragment = new ProfilePageFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.Fragment_Container, selectedFragment).commit();
            return true;
        }


    });


    mBottomNav.setItemIconTintList(null);

//activity_main.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/Bottom_Navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_navigation"
    android:backgroundTint="@color/colorBlack"
    android:background="@drawable/ripple_effect_raised"
    app:itemIconTint="#ffffff"
    app:itemTextAppearanceActive="@style/navTextActive"
    app:itemTextAppearanceInactive="@style/navTextInactive"

    app:itemTextColor="#ff0000"/>

//Ripple.xml(新闻效果)

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorWhite">

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/black" />
        <corners android:radius="7dp" />

    </shape>
</item>

//已更新

//Activity.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/Bottom_Navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_navigation"

    app:itemTextAppearanceActive="@style/navTextActive"
    app:itemTextAppearanceInactive="@style/navTextInactive"
    app:itemIconTint="#ffffff"
    app:itemTextColor="#FFFFFF"

    android:background="@drawable/shape"
    app:itemRippleColor="@color/bottomnav_ripple"
    />

//bottomnav_ripple.xml(我把它放在res/color中)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Selected. -->

<item android:alpha="0.68" android:color="#21FFFF" android:state_pressed="true" android:state_selected="true"/>
<item android:alpha="0.66" android:color="#21FFFF" android:state_focused="true" android:state_hovered="true" android:state_selected="true"/>
<item android:alpha="0.62" android:color="#21FFFF" android:state_focused="true" android:state_selected="true"/>
<item android:alpha="0.54" android:color="#21FFFF" android:state_hovered="true" android:state_selected="true"/>
<item android:alpha="0.50" android:color="#21FFFF" android:state_selected="true"/>

<!-- Unselected. -->

<item android:alpha="0.68" android:color="#FFFFFF" android:state_pressed="true"/>
<item android:alpha="0.66" android:color="#FFFFFF" android:state_focused="true" android:state_hovered="true"/>
<item android:alpha="0.62" android:color="#FFFFFF" android:state_focused="true"/>
<item android:alpha="0.54" android:color="#FFFFFF" android:state_hovered="true"/>
<item android:alpha="0.50" android:color="#FFFFFF"/>

//shape.xml(我把它放在res/drawable中)(如果我在shape.xml中使用黑色,颜色仍然不可见,所以我尝试使用紫色,但它仍然是灰色的)

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">
<solid android:color="@android:color/holo_purple" />
<corners android:radius="7dp" />
</shape>

it still grey

java android bottomnavigationview material-components material-components-android
2个回答
1
投票

仅将

android:background
用于形状,使用
app:itemRippleColor
定义项目上的波纹:

  <com.google.android.material.bottomnavigation.BottomNavigationView
      android:background="@drawable/shape"
      app:itemRippleColor="@color/bottomnav_ripple"
      ../>

其中

shape.xml
是:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <solid android:color="@android:color/black" />
  <corners android:radius="7dp" />
</shape>

bottomnav_ripple.xml
类似:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Selected. -->

<item android:alpha="0.68" android:color="#21FFFF" android:state_pressed="true" android:state_selected="true"/>
<item android:alpha="0.66" android:color="#21FFFF" android:state_focused="true" android:state_hovered="true" android:state_selected="true"/>
<item android:alpha="0.62" android:color="#21FFFF" android:state_focused="true" android:state_selected="true"/>
<item android:alpha="0.54" android:color="#21FFFF" android:state_hovered="true" android:state_selected="true"/>
<item android:alpha="0.50" android:color="#21FFFF" android:state_selected="true"/>

    <!-- Unselected. -->

<item android:alpha="0.68" android:color="#FFFFFF" android:state_pressed="true"/>
<item android:alpha="0.66" android:color="#FFFFFF" android:state_focused="true" android:state_hovered="true"/>
<item android:alpha="0.62" android:color="#FFFFFF" android:state_focused="true"/>
<item android:alpha="0.54" android:color="#FFFFFF" android:state_hovered="true"/>
<item android:alpha="0.50" android:color="#FFFFFF"/>

</selector>

根据需要更改颜色和 Alpha 通道。

enter image description here


0
投票

直到最近,这个问题可以通过将“@android:color/transparent”应用到“app:itemBackground”或“app:itemRippleColor”来解决。

但是,BottomNavigation 样式似乎发生了变化,之前的方法不再起作用。我找到的解决方案是对“app:itemActiveIndicatorStyle”应用透明颜色。

<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemActiveIndicatorStyle="@android:color/transparent" 
...
/>
© www.soinside.com 2019 - 2024. All rights reserved.