Android TextView Drawable 居中

在 Android 开发中,TextView 是常用的控件之一,用于显示文本内容。除了文本之外,TextView 还可以显示图标,这些图标被称为 Drawable。然而,默认情况下,TextView 中的 Drawable 是左对齐的,而不是居中对齐的。本文将介绍一种实现将 TextView 中的 Drawable 居中的方法,并提供相应的代码示例。

方法一:使用 Compound Drawables

首先,我们可以使用 Compound Drawables 的方式来实现将 TextView 中的 Drawable 居中。Compound Drawables 是指将图标放置在 TextView 的四个方向(上、下、左、右)的方法。我们可以通过设置 Compound Drawables 来实现 Drawable 的居中效果。

TextView textView = findViewById(R.id.text_view);
Drawable[] drawables = textView.getCompoundDrawables();

for (Drawable drawable : drawables) {
    if (drawable != null) {
        Rect bounds = drawable.getBounds();
        int textHeight = textView.getLineHeight();
        int drawableHeight = bounds.bottom - bounds.top;

        // 计算居中偏移量
        int offset = (textHeight - drawableHeight) / 2;

        // 设置居中偏移量
        bounds.top += offset;
        bounds.bottom += offset;

        // 设置 Drawable 的边界
        drawable.setBounds(bounds);
    }
}

上述代码中,我们首先获取了 TextView 中的四个方向的 Drawables,然后遍历每一个 Drawable。对于每个 Drawable,我们首先通过 getBounds() 方法获取其边界,然后计算出文本高度和 Drawable 高度之间的差值,这个差值就是居中的偏移量。接下来,我们将偏移量应用到 Drawable 的边界中,通过 setBounds() 方法设置 Drawable 的边界。

方法二:通过自定义 TextView

除了使用 Compound Drawables 的方式,我们还可以通过自定义 TextView 来实现将 Drawable 居中的效果。这种方式相对复杂一些,但是更加灵活,可以适应更多的场景。

首先,我们需要创建一个自定义的 TextView 类,继承自 AppCompatTextView。

public class CenterDrawableTextView extends AppCompatTextView {

    public CenterDrawableTextView(Context context) {
        super(context);
    }

    public CenterDrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CenterDrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        for (Drawable drawable : drawables) {
            if (drawable != null) {
                Rect bounds = drawable.getBounds();
                int textHeight = getLineHeight();
                int drawableHeight = bounds.bottom - bounds.top;

                // 计算居中偏移量
                int offset = (textHeight - drawableHeight) / 2;

                // 设置居中偏移量
                bounds.top += offset;
                bounds.bottom += offset;

                // 设置 Drawable 的边界
                drawable.setBounds(bounds);
            }
        }
        super.onDraw(canvas);
    }
}

上述代码中,我们通过继承 AppCompatTextView,并重写 onDraw() 方法。在 onDraw() 方法中,我们获取了 TextView 中的四个方向的 Drawables,然后遍历每一个 Drawable,并进行居中的计算和设置。

接下来,我们可以在布局文件中使用我们自定义的 TextView。

<com.example.myapplication.CenterDrawableTextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:drawableLeft="@drawable/ic_android"
    android:drawablePadding="8dp" />

上述代码中,我们通过 android:drawableLeft 属性设置了左侧的 Drawable,通过 android:drawablePadding 属性设置了 Drawable 和文本之间的间距。

总结

本文介绍了两种实现将 TextView 中的 Drawable 居中的方法。第一种方法是使用 Compound Drawables,通过计算偏移量并设置 Drawable 的边界完成居中效果。第二种方法是通过自定义 TextView,重写 onDraw() 方法,在绘制文本之前完成居中计算和设置。这两种方法各有优缺点,开发者可以根据实际需求选择适合的方法。

希望本文对您理解 Android TextView Drawable 居中