[英]How to bring the Badge Count in front of the Button in android?
我试图在按钮的右上角显示通知数量,我正在使用 ViewBadger(一个外部库:android-viewbadger http://jgilfelt.github.io/android-viewbadger )。 我现在面临的问题是徽章像这样显示在按钮的背面
但我想像 facebook 或任何其他应用程序一样在前面显示徽章
这是我的代码
package com.daimkhan.badgerview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.readystatesoftware.viewbadger.BadgeView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.button);
final BadgeView badge = new BadgeView(this, button);
badge.setText("8");
badge.setTextSize(12);
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.setBadgeMargin(0, 0);
badge.show();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
badge.hide();
}
});
}
}
请帮助我,在此先感谢:)
在显示徽章之前试试这个
ViewCompat.setTranslationZ(badge, 10);
这会更改视图相对于 Z 轴的高度。
官方文档说
setTranslationZ
void setTranslationZ (View view, float translationZ)
Sets the depth location of this view relative to its elevation.
使用您提供的相同代码,它对我来说很好用;
尝试将其用作您的布局文件(可能是您的布局有问题):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"/>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.