gpt4 book ai didi

android - 有没有办法在 API 25 中引入的 Google 官方 BottomNavigationView 菜单项上显示通知徽章?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:21 25 4
gpt4 key购买 nike

我一直在尝试在 API 25 中发布的 BottomNavigationView。我想在底部导航中的一个菜单项上显示一个通知标记(比如一个带有或不带有计数的蓝色小圆圈)酒吧。

我有一个选择器可绘制对象,我在其中添加了 checked true 和 checked false 状态,并带有灰色的可绘制对象,上面有一个蓝点。当用户导航到其他导航项时,整个菜单按钮和徽章都会变成灰色。我知道这是因为 itemIconTint 应用于可绘制对象,这就是将不同颜色的徽章作为图标的一部分不起作用的原因。有没有其他方法可以实现这一目标?

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
app:itemIconTint="@color/selector_bottom_nav"
app:itemTextColor="@color/selector_bottom_nav"
android:layout_gravity="bottom"
app:menu="@menu/menu_bottom_nav">
</android.support.design.widget.BottomNavigationView>

这就是我使用它的方式。删除 itemIconTint 并以编程方式更改可绘制图标没有帮助。

在 Android Developers 上我没有发现任何东西,而且它是非常新的,在网络上也没有任何可用的东西。

底部导航栏有自定义库,但我正在寻找官方库的支持。

任何想法,任何人?

最佳答案

回答我自己的问题:

我最终在我的布局中放置了 2 个带有徽章可绘制和可见性 GONEImageViews。我这样计算徽章的位置:

private void calculateBadgesPosition() {
int totalNavItems = 3;
for(int i = 0 ; i < bottomNavigation.getChildCount() ; i++) {
View view = bottomNavigation.getChildAt(i);

// Since Y remains same for all badges at least in my case.
// 1.3 is a factor that fits my placement of badge.
double y = getResources().getDisplayMetrics().heightPixels - (view.getMeasuredHeight() * 1.3);
if(image2ndItemBadge != null) {
float x = getResources().getDisplayMetrics().widthPixels/2 + imageClassroomBadge.getMeasuredWidth()/2;

image2ndItemBadge.setX(x);
image2ndItemBadge.setY((float)y);
image2ndItemBadge.requestLayout();
}
// Since BottomNavigationView items are equally distributed you can find
// the right position for 3rd item (in my case last item) by dividing
// BottomNavigationView width by 3 and then by 2 to get the middle of that
// item, which was needed in my case.
if(image3rdItemBadge != null) {
float x = getResources().getDisplayMetrics().widthPixels - ((view.getMeasuredWidth()/totalNavItems)/2);

image3rdItemBadge.setX(x);
image3rdItemBadge.setY((float)y);
image3rdItemBadge.requestLayout();
}
// BottomNavigationView count should always be 1
// but I observed the count was 2 in API 19. So I break after first iteration.
break;
}
}

您可以删除 for 循环,只检查子项计数是否大于 0 并获取该子项。我在 onCreate 的末尾调用上述方法,如下所示:

ViewTreeObserver viewTreeObserver = getWindow().getDecorView().getViewTreeObserver();
viewTreeObservier.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
calculateBadgesPosition();
getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

Badge ImageView 不是 BottomNavigationView 的一部分可绘制的项目不会受到 BottomNavigationView 应用于选择/取消选择项目的色调的影响.此外,如果您发现您的徽章没有出现,请尝试给它更高的 elevation 8 或 16 或其他值。在我的例子中,我的徽章留在 BottomNavigationView 后面可能是因为它具有更高的高度或 Z 索引。

我用 3 种不同的屏幕尺寸对此进行了测试,所有屏幕的位置都相同。

我希望这可以帮助任何遇到与官方 BottomNavigationView 类似问题的人。

另外,如果您有更好的方法,请分享。

关于android - 有没有办法在 API 25 中引入的 Google 官方 BottomNavigationView 菜单项上显示通知徽章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42377819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com