覆盖在 Android 上的 ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20804564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overlay over an ImageView on Android
提问by cesarferreira
So I have a list of images that come from the web, I don't know which color are they and I want to place a text over the ImageView.
所以我有一个来自网络的图像列表,我不知道它们是什么颜色,我想在 ImageView 上放置一个文本。
My idea is to place the ImageView, an image overlay with transparency gradient over that ImageView and the text above it. I want to mimic this behaviour:
我的想法是放置 ImageView,这是一个图像叠加层,在该 ImageView 及其上方的文本上具有透明度渐变。我想模仿这种行为:
Is there anyway to do this via XML?
有没有办法通过 XML 做到这一点?
回答by adityajones
When you write the XML for your list items which get inflated in the getView(...)
of whatever ListAdapter
you've written you can surely do this.
当您为列表项编写 XML 时,这些列表项在您编写getView(...)
的任何ListAdapter
内容中都被夸大了,您肯定可以做到这一点。
Something like this for the list item:
列表项是这样的:
<?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>
Then you create that drawable/gradient. For that you can recycle the answer from here.
然后创建可绘制/渐变。为此,您可以从这里回收答案。
回答by cesarferreira
Thanks to adityajonesI managed to get there :) So although this is myright answer, I'll mark his as the correct one!
感谢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>
回答by Matt
I'd use a FrameLayout or RelativeLayout. The first View you add to either should be the background ImageView, then obviously you'll need some TextViews and Other ImageViews [or Buttons, or ImageButtons, etc]
我会使用 FrameLayout 或 RelativeLayout。您添加的第一个视图应该是背景 ImageView,然后显然您需要一些 TextViews 和其他 ImageViews [或按钮,或 ImageButtons 等]
Seems like a reasonable layout: a background image, and then one additional view in each corner.
看起来是一个合理的布局:一个背景图像,然后在每个角落都有一个额外的视图。
For the gradient, you'll probably want a separate Layout/View at the bottom with a gradient drawable as the background, although I can imagine you might be able to get away with setting the background of one of your TextViews as the gradient.
对于渐变,您可能希望底部有一个单独的布局/视图,可绘制渐变作为背景,尽管我可以想象您可以将其中一个 TextView 的背景设置为渐变。
回答by Khaled AbuShqear
You do not have to use a gradient drawable file or set it in your xml..
you can do this pragmatically using GradientDrawable
Class as explained in this related Question (Create a radial gradient programmatically) then set it as a background for a layout that covers your ImageView, this gives you ability to use different colors and orientations
您不必使用渐变可绘制文件或将其设置在您的 xml 中。您可以GradientDrawable
按照相关问题(以编程方式创建径向渐变)中的说明使用Class实用地执行此操作,然后将其设置为覆盖您的布局的背景ImageView,这使您能够使用不同的颜色和方向