75

Android中是否可以有重叠的视图?我想要一个 ImageView,前面有一个透明的 png,背景有另一个视图。

编辑:

这就是我目前的情况,问题是imageView中的图像不透明,应该透明的部分只是黑色。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gallerylayout"
>
<Gallery android:id="@+id/overview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

 <ImageView android:id="@+id/navigmaske"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/navigmask"
    /> 

</RelativeLayout>

编辑:

我让它工作了,它是团队中另一个程序员的主题文件。刚改了这个

<item name="android:background">#FF000000</item>

对此

<item name="android:background">#00000000</item>
4

7 回答 7

71

ImageViewAndroid 本机处理视图和可绘制对象(包括 PNG 图像)之间的透明度,因此您描述的场景(在 a 前面部分透明Gallery)当然是可能的。

如果您遇到问题,可能与布局或图像有关。我已经复制了您描述的布局并成功实现了您所追求的效果。这是我使用的确切布局。

<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/gallerylayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Gallery
    android:id="@+id/overview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />
  <ImageView
    android:id="@+id/navigmaske"
    android:background="#0000"      
    android:src="@drawable/navigmask"
    android:scaleType="fitXY"
    android:layout_alignTop="@id/overview"
    android:layout_alignBottom="@id/overview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />
</RelativeLayout>

请注意,我已将父级更改为通常您想要的主 ActivityRelativeLayout的高度和宽度。fill_parent然后我将顶部和底部与顶部和底部对齐,ImageViewGallery确保它在它前面居中。

我还明确地将背景设置ImageView为透明。

至于可绘制图像本身,如果您将 PNG 文件放在某个地方供我查看,我可以在我的项目中使用它,看看它是否负责。

于 2009-06-08T15:59:36.830 回答
9

另外,请看一下FrameLayout,这就是 Camera 的 Gallery 应用程序实现缩放按钮覆盖的方式。

于 2009-06-08T13:45:43.963 回答
7

如果您想在布局上添加自定义叠加屏幕,您可以创建自定义线性布局并控制绘图和关键事件。你可以我的教程-Android 布局上的叠加-http: //prasanta-paul.blogspot.com/2010/08/overlay-on-android-layout.html

于 2010-08-12T03:43:46.380 回答
1

可见画廊改变可见性,这是​​您如何让画廊超越其他视图重叠。Home 示例应用程序有一些很好的例子。

于 2009-06-07T17:31:19.640 回答
1

最简单的方法是将 -40dp 边距放在顶部图像视图的底部

于 2014-11-06T13:54:34.487 回答
0

现在在 android 中使用 Jetpack Compose,您应该使用 Box 来重叠视图。例子。

Box(modifier = Modifier.fillMaxWidth().fillMaxHeight()){
        RecipesList(viewModel.recipes.value)
        Snackbar()
    }

在这里,RecipesList 和 Snackbar 是组合物,它们按组合顺序排列在另一个之上

查看 Jetpack Compose 示例 - https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/

于 2021-08-29T16:03:36.477 回答
-3

是的,这是可能的。然而,挑战在于正确地进行布局。最简单的方法是拥有一个 AbsoluteLayout,然后将两个图像放在您想要的位置。除了稍后将其添加到布局之外,您不需要对透明 png 执行任何特殊操作。

于 2009-06-07T15:04:11.673 回答