安卓xml布局
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="14dp"
android:layout_alignParentEnd="true"
android:background="@drawable/bg_red_point_4"
android:gravity="center"
android:minWidth="14dp"
android:paddingStart="3dp"
android:paddingEnd="3dp"
android:text="3"
android:textColor="@color/white"
android:textSize="10sp" />
Java代码
TextView mTextMessageNumber = new TextView(mContext);
LayoutParams params4 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dp2px(14));
params4.setMargins(mIconSize/2+2, dp2px(3), 0, 0);
mTextMessageNumber.setLayoutParams(params4);
mTextMessageNumber.setTextSize(TypedValue.COMPLEX_UNIT_PX,dp2px(10));
mTextMessageNumber.setTextColor(ColorUtil.getColor(R.color.white));
mTextMessageNumber.setGravity(Gravity.CENTER);
mTextMessageNumber.setBackgroundResource(R.drawable.bg_red_point_4);
mTextMessageNumber.setMinWidth(dp2px(14));
mTextMessageNumber.setPadding(dp2px(3),0,dp2px(3),0);
if (mNumber.isEmpty()){
mTextMessageNumber.setVisibility(GONE);
}else {
if (mNumber.equals("0")){
mTextMessageNumber.setVisibility(GONE);
}else {
mTextMessageNumber.setText(mNumber);
}
}
bg_red_point_4.xml的代码
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="7dp"/> <solid android:color="#FFFF495D"/> <stroke android:width="1dp" android:color="@color/white"/> </shape>