我想在 android 中显示计数徽章在 float 操作按钮前面。我使用 FrameLayout 来实现这一点。我的代码在这里
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/send_button"
android:layout_toLeftOf="@+id/send_button"
android:layout_toStartOf="@+id/send_button"
android:layout_gravity="end|bottom"
android:id="@+id/frameLayout">
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/listen_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:backgroundTint="#e3e3e3" />
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="#FFF"
android:textSize="10sp"
android:textStyle="bold"
android:background="@drawable/badge_circle"
android:layout_gravity="right|bottom"
android:layout_alignBottom="@+id/frameLayout"
android:layout_toLeftOf="@+id/frameLayout"
android:layout_toStartOf="@+id/frameLayout" />
</FrameLayout>
我在FAB下面得到了计数徽章,如下图
我需要计数徽章在 FAB 前面。
最佳答案
您可以使用 BadgeDrawable
由 Material 组件库提供。
类似于:
fab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setNumber(2);
//Important to change the position of the Badge
badgeDrawable.setHorizontalOffset(30);
badgeDrawable.setVerticalOffset(20);
BadgeUtils.attachBadgeDrawable(badgeDrawable, fab, null);
fab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
目前 (1.3.0-alpha02
) 没有改变半径的方法,但是你可以在你的项目中覆盖这个维度 (dimens.xml
) :
<dimen name="mtrl_badge_with_text_radius">12dp</dimen>
关于android - float 操作按钮上的徽章计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639698/