【问题标题】:TextView with a background and Transparent Text具有背景和透明文本的 TextView
【发布时间】:2013-10-28 09:16:50
【问题描述】:

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

目前,我正在尝试使用 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>

但是,它显示以下内容:

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

所以我的问题是:

  • 我怎样才能做到这一点?
  • 帆布?跨度?我不习惯这些方法,因此不胜感激。

谢谢!!

编辑: 蓝色背景必须是动态的,并且可能是图像!

【问题讨论】:

  • 你会有动态背景吗?如果不只是将文本颜色设置为背景颜色。
  • 背景很可能是一张图片......所以,它完全是动态的
  • 我已经编辑了我的问题。请检查编辑评论。
  • @Reinherd:你正在寻找我的this answer
  • @GilVegliach 我是* 3 年前:D

标签: android android-layout


【解决方案1】:

您可以为此使用 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);

【讨论】:

  • 我不习惯使用画布。如何将此代码添加为视图?
  • 您可以在布局中添加一个 ImageView,然后:ImageView image = (ImageView)getActivity().findViewById(R.id.imageView1);image.setImageBitmap(bmOverlay);
  • 我已经试过了,文本,如果它是透明的,它是不可见的,所以它不起作用。
  • 试试这个:android.okhelp.cz/…
  • 好的,它适用于:paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
【解决方案2】:

textcolor 设置为您的布局backgound 颜色如浅天蓝色..

【讨论】:

    【解决方案3】:

    你不能在 Xml 布局中实现这一点,你需要为布局背景和文本设置相同的颜色,如下所示:

    <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="#0011ff" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffcecece"
        android:padding="5dp"
        android:text="@string/hello_world"
        android:textColor="#0011ff" />
    </RelativeLayout>
    

    【讨论】:

    • 不是我需要的。检查我的编辑。
    • 您可以在您想要的 java 类中通过设置 backgroundResource 来执行此操作,如下所示:mRelativeLayout.setBackgroundResource(R.drawable.imagename);
    • 这与图像无关。这是关于将文本设置为透明,因此您可以“通过” TextView 阅读
    • 你不明白我在做什么。请再次检查问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多