1、创建一个 XML 文件在 res/drawable
目录下(默认为黑色)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#000000" />
<size
android:width="24dp"
android:height="24dp" />
</shape>
2、创建布局
// 创建一个水平的LinearLayout来包含“故障”文本和圆点图片
final LinearLayout statusLayout = new LinearLayout(context);
statusLayout.setOrientation(LinearLayout.HORIZONTAL);
// 添加圆点图片
final ImageView dotImage = new ImageView(context);
LinearLayout.LayoutParams dotImageParams = new LinearLayout.LayoutParams(
10, // 设置宽度为10dp
10 // 设置高度为10dp
);
dotImageParams.setMargins(0, 0, 5, 0);
dotImage.setLayoutParams(dotImageParams);
// 添加圆点图片到statusLayout
dotImage.setImageResource(R.drawable.circle);
statusLayout.addView(dotImage);
// 添加文本
final TextView statusText = new TextView(context);
textView.setWidth(100);
// 添加文本到statusLayout
statusLayout.addView(statusText);
textViews[index] = statusText;
dotImages[index] = dotImage;
3、调接口后更新状态
int idleColor = Color.parseColor(color);
dotImages[index].setColorFilter(new PorterDuffColorFilter(idleColor, PorterDuff.Mode.SRC_IN));
textViews[index].setText(name);