Android 在 facebox 上布局将性别图标与年龄对齐

Android Layout align gender icon with age on facebox

我使用 canvas 图像在面部检测区域绘制矩形并在顶部绘制年龄文本。

Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
Canvas canvas = new Canvas(bitmap);
canvas.drawRect(left, top, left + width, top + height, paint);
canvas.drawText(myText, left + width / 2,(float) (top - 10), paint);

现在我正在尝试将性别图标与显示年龄的图像中的年龄文本对齐。 另一个问题是面部检测框可能很小,我如何相应地调整我的 TextView(icon + age) 的大小。

    TextView txtView=new TextView(getApplicationContext());
    txtView.setText(String.valueOf((int)json.getDouble("age")));
    txtView.setTextColor(getResources().getColor(R.color.red));
    txtView.setBackgroundColor(getResources().getColor(R.color.grey_dark));
    if (json.getDouble("sex") >= 0.5) {
        txtView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.maleicon, 0, 0, 0);
    } else {
        txtView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.femaleicon, 0, 0, 0);
    }
    LayoutParams layoutParams = new LayoutParams(150, 110);
    txtView.setLayoutParams(layoutParams);
    LL.addView(txtView);

使用框架布局(图像上的相对布局)并对齐它。然后以不同的方式调整图像的大小,并将其放入带有文本视图的线性布局中。