我有一个来自网络的图片列表,不知道它们是什么颜色,我想在ImageView上放置文本。
我的想法是在ImageView上放置一张带透明度渐变的图像叠加层,再将文本放在其上方。我想要模仿这种行为:
有没有办法通过XML实现这个效果?
当您编写XML以填充在您编写的任何ListAdapter
的getView(...)
中膨胀的列表项时,您肯定可以这样做。
例如,对于列表项可以使用以下内容:
<?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>
然后您创建可绘制/渐变。 您可以回收来自此处的答案。
感谢 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>
您不必使用渐变可绘制文件或在xml中设置它。您可以使用GradientDrawable
类以编程方式执行此操作,如相关问题(以编程方式创建径向渐变)中所述,然后将其设置为覆盖ImageView的布局的背景,这使您能够使用不同的颜色和方向。