导航抽屉中的通知徽章

时间:2018-09-07 07:44:03

标签: android xml android-layout

我试图在导航抽屉中添加圆形通知徽章,这是我的自定义文本视图。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:padding="2dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body2"
android:textColor="@color/colorWhite"
android:textStyle="bold" />

结果:

enter image description here

必填:

enter image description here

注意: 如果我将textview的高度设置为wrap_content,则textview将是圆形的,但它会粘在顶部。

enter image description here

4 个答案:

答案 0 :(得分:1)

对每行使用相对布局,然后在textView的XML中使用property属性center_vertical =“ true”和alignParentRight =“ true

答案 1 :(得分:1)

它是Android中的错误。您只能通过反复试验来实现。

答案 2 :(得分:0)

您必须在dp中修复文本视图的高度和宽度

答案 3 :(得分:0)

我刚刚创建了一个新的View类来实现此目的:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.design.widget.NavigationView;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;

public class DrawerBadge extends AppCompatTextView {

    private float strokeWidth;

    int strokeColor = Color.parseColor("#000000"); // black

    int solidColor = Color.parseColor("#FF0000"); // red

    // **** THIS IS THE FULL CONSTRUCTOR YOU HAVE TO CALL ****
    public DrawerBadge(Context context, NavigationView navigationView, int idItem, String value, String letterColor, String strokeColor, String solidColor) {
        super(context);
        MenuItemCompat.setActionView(navigationView
                .getMenu().findItem(idItem), this);
        DrawerBadge badge = (DrawerBadge) MenuItemCompat
                .getActionView(navigationView
                        .getMenu().findItem(idItem));
        badge.setGravity(Gravity.CENTER);
        badge.setTypeface(null, Typeface.BOLD);
        badge.setTextColor(Color.parseColor(letterColor));
        badge.setText(value);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        badge.setLayoutParams(params);
        badge.setPadding(3,3,3,3);
        badge.setStrokeWidth(1);
        badge.setStrokeColor(strokeColor);
        badge.setSolidColor(solidColor);
        badge.requestLayout();
    }

    public DrawerBadge(Context context) {
        super(context);
    }

    public DrawerBadge(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawerBadge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void draw(Canvas canvas) {

        Paint circlePaint = new Paint();
        circlePaint.setColor(solidColor);
        circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint strokePaint = new Paint();
        strokePaint.setColor(strokeColor);
        strokePaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        int  h = this.getHeight();
        int  w = this.getWidth();

        int diameter = ((h > w) ? h : w);
        int radius = diameter/2;

        this.setHeight(diameter);
        this.setWidth(diameter);

        canvas.drawCircle(diameter / 2 , diameter / 2, radius, strokePaint);

        canvas.drawCircle(diameter / 2, diameter / 2, radius-strokeWidth, circlePaint);

        super.draw(canvas);
    }

    public void setStrokeWidth(int dp)
    {
        float scale = getContext().getResources().getDisplayMetrics().density;
        strokeWidth = dp*scale;

    }

    public void setStrokeColor(String color)
    {
        strokeColor = Color.parseColor(color);
    }

    public void setSolidColor(String color)
    {
        solidColor = Color.parseColor(color);

    }
}

如何使用?像这样调用DrawerBadge FULL CONSTRUCTOR

DrawerBadge badge = new DrawerBadge(this, findViewById(R.id.navigation_view), 
                  R.id.menu_item_id, 
                  String.valueOf(count > 99 ? "+" + 99 : count), 
                  "#FFFFFF","#FF0000","#FF0000");

享受!