想要一个完美的圆圈来进行通知计数 [英] want perfect circle for notification count

查看:54
本文介绍了想要一个完美的圆圈来进行通知计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助以下代码,我已经在选项菜单上实现了通知计数的代码

I have implemented code for notification count on option menu with the help of below code

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main_activity_navigation, menu);

        MenuItem menuItem = menu.findItem(R.id.action_settings);
        menuItem.setIcon(buildCounterDrawable(4, android.R.color.transparent));


        return true;
    }

    private Drawable buildCounterDrawable(int count, int backgroundImageId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.noitification_count, null);
        view.setBackgroundResource(backgroundImageId);

        if (count == 0) {
            View counterTextPanel = view.findViewById(R.id.badge_layout1);
            counterTextPanel.setVisibility(View.GONE);
        } else {
            TextView textView = (TextView) view.findViewById(R.id.badge_notification_1);
            textView.setText("" + count);
        }

        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        return new BitmapDrawable(getResources(), bitmap);
    }

这是我的xml文件(notification_count.xml)

here is my xml file (notification_count.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@android:style/Widget.ActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="true"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/badge_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/relative_layout_item_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button1"
                android:layout_width="@dimen/_100sdp"
                android:layout_height="@dimen/_100sdp"
                android:background="@drawable/ic_dollar" />
        </RelativeLayout>

        <TextView
            android:id="@+id/badge_notification_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/relative_layout_item_count"
            android:background="@drawable/circle_bg"
            android:gravity="center"
            android:text="12"
            android:textColor="#FFF"
            android:padding="@dimen/_10sdp"
            android:textSize="@dimen/_30sdp"
            android:textStyle="bold" />
    </RelativeLayout>
        </RelativeLayout>

这是我的circle_bg.xml

here is my circle_bg.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="@color/green"/>

    <size
        android:width="20dp"
        android:height="20dp"/>
</shape>

但是我在通知数量上并没有达到理想的水平.我越来越像下面

But i am not getting perfect circle on notification count. I am getting like below

但我想完美地圈出通知数.所以请帮助我.

but i want perfectly circle for notification count. so please help me.

推荐答案

为徽章设置固定的宽度和高度 View

set fixed width and height for your badge View

<TextView
        android:id="@+id/badge_notification_1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignRight="@id/relative_layout_item_count"
        android:background="@drawable/circle_bg"
        android:gravity="center"
        android:text="12"
        android:textColor="#FFF"
        android:padding="@dimen/_10sdp"
        android:textSize="@dimen/_30sdp"
        android:textStyle="bold" />

从您的形状中删除,在这种情况下,不需要使用它

and remove from your shape, its uncessary in this case

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@color/green"/>
</shape>

这篇关于想要一个完美的圆圈来进行通知计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆