【发布时间】:2015-12-11 01:23:53
【问题描述】:
我正在尝试为图像顶部和底部的图像视图添加渐变。我不想在图像视图之上添加文本视图。如何实现?
【问题讨论】:
-
如果你想在另一个视图上显示一些东西,你必须使用
RelativeLayout并覆盖视图。
标签: android
我正在尝试为图像顶部和底部的图像视图添加渐变。我不想在图像视图之上添加文本视图。如何实现?
【问题讨论】:
RelativeLayout 并覆盖视图。
标签: android
看来您简单而干净的解决方案是将 ImageView 与 FrameLayout 包装并使用前景属性如下:
布局
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/gradient">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/male"/>
</FrameLayout>
还有渐变以防万一
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#33000000"
android:centerColor="@android:color/transparent"
android:endColor="#33000000"
android:angle="90"/>
</shape>
产生这个:
FrameLayout 是相对简单的结构,不会增加视图层次结构的复杂性,而相对于 RelativeLayout,例如会导致额外的测量。
如果您绝对必须避免添加任何额外的包装视图,那么您的下一个选择是创建一个 CustomView,从 ImageView 扩展并覆盖 onDraw() 方法,并使用渐变位图中的像素手动覆盖现有画布。
【讨论】: