![](/img/trans.png)
[英]How can I use a custom image as BadgeDrawable in a BottomNavigationView?
[英]BadgeDrawable does not appear on elements other than 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 只是因為這個問題修復似乎還沒有發布。
布局:
<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);
}
});
現在我想出了這個 function:
fun addBadgeDrawable(count: Int, target: View, parent: FrameLayout, context: Context) {
target.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val badgeDrawable = BadgeDrawable.create(context)
badgeDrawable.number = count
badgeDrawable.setBoundsFor(target, parent)
parent.foreground = badgeDrawable
target.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
不幸的是, BadgeUtils
目前不適合我。
這是布局:
<FrameLayout
android:id="@+id/issues_button_container"
android:layout_width="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_gravity="right"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingEnd="12dp">
<ImageView
android:id="@+id/issues_button"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_warning_black_24dp"
android:tint="@color/c_gold" />
</FrameLayout>
我懷疑在不久的將來這個解決方案會出現問題。 如果我能找到任何答案,我會嘗試更新這個答案。
該解決方案主要受此答案的啟發。
FrameLayout frameLayout = findViewById(R.id.frameLayout)
ImageView imageView = findViewById(R.id.ivIcon);
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
badgeDrawable.setNumber(32);
badgeDrawable.setBadgeGravity(BadgeDrawable.BOTTOM_END);
BadgeUtils.attachBadgeDrawable(badgeDrawable, imageView, framelayout);
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
根據您的徽章可繪制的重力,相應地設置填充。 就我而言,我希望徽章顯示在底端。
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:layout_centerInParent="true"
android:layout_gravity="end"
android:paddingBottom="9dp"
android:paddingEnd="13dp"
>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/ivIcon"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_icon" />
</FrameLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.