android - 带有背景和透明文本的 TextView

标签 android android-layout

我正在尝试执行以下操作:

enter image description here

目前,我正在尝试使用 NativeAndroid。

不过,我可以使用 HTML 和 css 来做到这一点。

如您所见,它类似于:

  • 背景为蓝色(可以是任何其他颜色,甚至是图像)的持有人
  • 带有(透明??)颜色的 TextView
  • 灰色背景的TextView

但是,如果我将背景设置为灰色并将文本设置为透明,则文本“消失”并且全部显示为灰色。

这是我的尝试(原生 Android):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcecece"
    android:padding="5dp"
    android:text="@string/hello_world"
    android:textColor="#00ffffff" />
</RelativeLayout>

但是,它显示以下内容:

enter image description here

有文字,但因为是透明的,所以没有显示。

所以我的问题是:

  • 我怎样才能做到这一点?
  • Canvas ?跨越?我不习惯这些方法,所以我将不胜感激。

谢谢!!

编辑: 那个蓝色背景一定是动态的,它可能是一张图片!

最佳答案

您可以为此使用 Canvas:

Bitmap bmOverlay = Bitmap.createBitmap(1200, 1200, Bitmap.Config.ARGB_8888);//bMap.getConfig());

Canvas canvas = new Canvas(bmOverlay);
canvas.drawColor(Color.LTGRAY);
Paint paint = new Paint();

Paint mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setStrokeWidth(3);
mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintText.setColor(Color.TRANSPARENT);
mPaintText.setTextSize(50f);
mPaintText.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));    
canvas.drawText("Your TExt", x, y, mPaintText);

关于android - 带有背景和透明文本的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630885/

相关文章:

java - 如何在android中以分钟和秒为单位获取音频长度

android - View.GONE 和 View.INVISIBLE 之间有什么区别

android - 处理 UI 事件的最佳实践

android - Android布局中的占位符文本(仅限设计过程)?

android - FragmentManager 不删除动态添加的 fragment

Android 在 ListView 中显示 HTML

android - 当布局本身的高度为零时,将子项的高度设置为 wrap_content

android - 使用自定义属性设置View子类的自定义属性?

android - 错误类型 3 - 启动 Activity 时出错

android - Flutter:如何使用 RaisingButton 在 PageView 中导航或使用按钮移动 pageViewController