gpt4 book ai didi

android - 想要通知计数的完美圆圈

转载 作者:搜寻专家 更新时间:2023-11-01 08:31:05 25 4
gpt4 key购买 nike

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

  @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)

<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

<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>

但是我在通知计数上没有得到完美的循环。我越来越像下面 getting this.

但我想要完美的圆圈来计算通知数量。所以请帮助我。

最佳答案

为徽章设置固定的宽度和高度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" />

并从你的形状中移除,在这种情况下它是不必要的

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

关于android - 想要通知计数的完美圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com