繁体   English   中英

使用自定义图标时增加导航底部视图android中所选图标的高度?

[英]Increase height of selected icon in navigation bottom view android when using custom icon?

拜托,我想像这样在android中使用自定义图标作为底部导航视图。 选中后如何更改图标位置? 当他们被选中时,他们会上升一点。 您是否创建了具有一定边距的选定图标,或者有没有办法在 android 中设置高度? 不过,这张图片来自一个颤振库。 我想在 Android Java 项目中重现它。 或者找一个实现它的库在此处输入图片说明

在您的bottomNavigationView.setOnNavigationItemSelectedListener ,每个按下的图标都会调用该动画方法animateBottomIcon(int itemIndex, boolean isChecked)

 BottomNavigationView bottomNav; BottomNavigationMenuView menuView; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { bottomNav.setClipChildren(false); bottomNav.setClipToPadding(false); bottomNav.setClipToOutline(false); menuView.setClipChildren(false); menuView = (BottomNavigationMenuView) bottomNav.getChildAt(0); } private void animateBottomIcon(int itemIndex, boolean isChecked) { final View view = menuView.getChildAt(itemIndex).findViewById(com.google.android.material.R.id.icon); ObjectAnimator translateUpAnimator = ObjectAnimator.ofFloat(view, "translationY", 0, (float) (-(bottomNav.getHeight() / 2))).setDuration(500); if(!isChecked) { translateUpAnimator.start(); } if(currentItemIndex != -1) { final View currentView = menuView.getChildAt(currentItemIndex).findViewById(com.google.android.material.R.id.icon); ObjectAnimator translateDownAnimator = ObjectAnimator.ofFloat(currentView, "translationY", 0, (float) (-(bottomNav.getHeight() / 2))).setDuration(500); if (!isChecked) { translateDownAnimator.reverse(); } } }

通常,BottomNavigation 会切断菜单图标以避免使用: android:clipChildren="false"在布局的根视图上,以及在 Java 类中的 onCreateView() 中:

bottomNav.setClipChildren(false); bottomNav.setClipToPadding(false); bottomNav.setClipToOutline(false);

最重要的是在您的 menuItem 上,因为它是您的图标项的父项。 menuView.setClipChildren(false); .

记住给你的底部导航视图一个固定的高度。

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_gravity="bottom"
    android:background="@color/white"
    app:itemIconSize="54dp"
    app:elevation="12dp"
    app:labelVisibilityMode="unlabeled"
    app:menu="@menu/menu_bottom_nav">[![enter image description here][1]][1]

更新:图书馆在这里住https://github.com/Kwasow/BottomNavigationCircles-Android

我偶然发现了你的问题,并创建了一个受设计启发的自定义 android 导航栏。

导航栏看起来像这样: 在此处输入图片说明

它的代码位于: https : //github.com/Kwasow/Archipelago/blob/main/app/src/main/java/com/github/kwasow/archipelago/views/CustomBottomNavigation.kt

bg_green_circle.xml可绘制对象如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="24dp"
        android:height="24dp"/>
    <solid android:color="#6CB86A"/>
</shape>

将来我可能会把它变成一个包,但它仍然需要一些工作。 如果发生这种情况,我会更新帖子。

您可以通过使用选择器来做到这一点。 为此,您需要两个单独的图像:1) 当 state selected 为 true 时表示您选择的图像 2) 默认表示未选择图标时。

XML

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@drawable/YOUR SELECTED IMAGE">

    </item>
    <item android:drawable="@drawable/YOUR DEFAULT IMAGE"></item>

</selector>

然后将此选择器用作底部导航视图的每个图像图标的可绘制对象。 您可以将选择器 a/c 中的其他属性添加到您的要求中。

我在那里看到了有趣的行为。 但似乎android原生底部导航栏不支持在栏视图之外制作项目视图。 也许您可以通过为导航栏提供自定义背景来作弊,其中 30% 的顶部是透明的,其余部分的颜色与普通导航栏一样。 我没有尝试过这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM