Android文字覆盖图像

And*_*oid 90 android android-image android-imageview

我有一个带有图像的imageView,在我要放置文本的图像上.我怎样才能做到这一点?

Ale*_*qui 158

这就是我做的方式,它完全按照你在RelativeLayout中的要求工作:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/myImageSouce" />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/myImageView"
        android:layout_alignTop="@id/myImageView"
        android:layout_alignRight="@id/myImageView"
        android:layout_alignBottom="@id/myImageView"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


mar*_*tar 15

你可能想从不同的方面采取if:在背景上使用带有drawable的TextView似乎更容易:

 <TextView
            android:id="@+id/text"
            android:background="@drawable/rounded_rectangle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        </TextView>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案的问题是你无法控制像imageView这样的背景属性.举例来说. (9认同)

Hak*_*bay 6

您希望使用FrameLayout或Merge布局来实现此目的.Android开发指南在这里有一个很好的例子:Android布局技巧#3:通过合并优化.


Chr*_*ris 6

你可能

  • 创建一个从类ImageView继承的新类
  • 覆盖方法onDraw.首先调用super.onDraw()该方法
  • 然后绘制一些想要显示的文本.

如果你这样做,你可以将它作为单个布局组件使用,这样可以更容易地与其他组件一起布局.


trg*_*lia 5

有很多方法.您使用RelativeLayout或AbsoluteLayout.

使用相对,您可以让图像与左侧的父对齐,并且也可以将文本与父对齐对齐...然后您可以在文本视图上使用边距和填充以及重力将其排列在您的位置想要超过图像.