在Android上对ImageView进行覆盖

14

我有一个来自网络的图片列表,不知道它们是什么颜色,我想在ImageView上放置文本。

我的想法是在ImageView上放置一张带透明度渐变的图像叠加层,再将文本放在其上方。我想要模仿这种行为:

输入图像描述

有没有办法通过XML实现这个效果?

4个回答

20

当您编写XML以填充在您编写的任何ListAdaptergetView(...)中膨胀的列表项时,您肯定可以这样做。

例如,对于列表项可以使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ImageView
    android:layout_width="320dp"
    android:layout_height="240dp"
    android:background="#ACACAC"/>

  <RelativeLayout
    android:layout_width="320dp"
    android:layout_height="240dp"
    android:background="@drawable/gradient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is your text"
        android:textColor="#000000"
        android:layout_alignParentBottom="true"/>

  </RelativeLayout>

</RelativeLayout>

然后您创建可绘制/渐变。 您可以回收来自此处的答案。


4

感谢 adityajones 的帮助,我终于弄明白了 :) 虽然这是 我的 正确答案,但我会标记他的回答为正确答案!

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:src="@drawable/image" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:background="@drawable/gradient_image" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="6dp"
        android:layout_marginBottom="18dp"
        android:shadowColor="#000"
        android:shadowRadius="7.0"
        android:text="This is some random text"
        android:textColor="@color/white"
        android:textSize="22sp" />
</RelativeLayout>


2
我会使用FrameLayout或RelativeLayout进行布局。你应该先添加背景ImageView,然后添加一些TextViews和其他ImageViews(或Buttons、ImageButtons等)。
这个布局看起来很合理:一个背景图片,每个角落还有一个额外的视图。
至于渐变效果,你可能需要在底部添加一个单独的Layout/View,并将其背景设置为渐变drawable。不过我可以想象你可能可以将其中一个TextView的背景设置为渐变效果。

0

您不必使用渐变可绘制文件或在xml中设置它。您可以使用GradientDrawable类以编程方式执行此操作,如相关问题(以编程方式创建径向渐变)中所述,然后将其设置为覆盖ImageView的布局的背景,这使您能够使用不同的颜色和方向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接