【发布时间】:2020-02-23 01:56:33
【问题描述】:
拜托,我想像这样在 android 的底部导航视图中使用自定义图标。选择时如何更改图标位置?当它们被选中时,它们会上升一点。您是否创建具有一定边距的选定图标,或者有没有办法在 android 中设置高度?不过,这张图片来自一个颤振库。我想在 Android Java 项目中重现它。或者找一个实现它的库
【问题讨论】:
标签: java android bottomnavigationview
拜托,我想像这样在 android 的底部导航视图中使用自定义图标。选择时如何更改图标位置?当它们被选中时,它们会上升一点。您是否创建具有一定边距的选定图标,或者有没有办法在 android 中设置高度?不过,这张图片来自一个颤振库。我想在 Android Java 项目中重现它。或者找一个实现它的库
【问题讨论】:
标签: java android bottomnavigationview
在您的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();
}
}
}
通常菜单图标将被底部导航切断以避免使用: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 导航栏。
bg_green_circle.xmldrawable 看起来像这样:
<?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)当状态选择为真时表示您选择的图像 2) default 表示没有选择图标时。
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 中添加其他属性。
【讨论】: