BadgeDrawable不会出现在BottomNavigationView以外的元素上 [英] BadgeDrawable does not appear on elements other than BottomNavigationView
本文介绍了BadgeDrawable不会出现在BottomNavigationView以外的元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法让BadgeDrawable工作。
与BottomNavigationView
navView.setupWithNavController(navController)
val badge = navView.getOrCreateBadge(R.id.navigation_home)
badge.number = 12
但当我想在其他元素上使用它时,它不会显示。如下所示:
val badgeDrawable = BadgeDrawable.create(context!!)
badgeDrawable.number = 32
BadgeUtils.attachBadgeDrawable(badgeDrawable, textView, root.findViewById(R.id.frame_layout))
我使用FrameLayout只是因为this issue fix似乎尚未发货。
布局:
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/text_home"
android:src="@drawable/ic_home_black_24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="20sp" />
</FrameLayout>
我使用的是可能的最新库版本:‘com.google.android.material:material:1.2.0-rc01’
我尝试过不同的方法。不过运气不好。有没有我遗漏的秘密?
以下是该项目的git回购(尽可能简单):https://github.com/marcinkunert/badgeissue
推荐答案
您可以使用如下内容:
<FrameLayout
android:clipChildren="false"
android:clipToPadding="false">
<ImageView
android:id="@+id/text_home"
.../>
</FrameLayout>
在您的代码中:
ImageView imageView = findViewById(R.id.text_home);
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
badgeDrawable.setNumber(32);
badgeDrawable.setBadgeGravity(BadgeDrawable.TOP_END);
BadgeUtils.attachBadgeDrawable(badgeDrawable, imageView, findViewById(R.id.layout));
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
这篇关于BadgeDrawable不会出现在BottomNavigationView以外的元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文