本文介绍了Android 中的重叠视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to have overlapping views in Android? I would like to have an ImageView with a transparent png in the front and another view in the background.

这就是我目前所拥有的,问题是imageView中的图像不透明,应该透明的部分只是黑色.

This is what I have at the moment, the problem is that the image in the imageView is not transparent, the parts that should be transparent are just black.

<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>

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

I got it to work, it was a theme file from another programmer on the team.Just changed this

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

到这里

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

推荐答案

Android 原生处理跨视图和可绘制对象(包括 PNG 图像)的透明度,因此您描述的场景(前面是部分透明的 ImageView画廊)当然是可能的.

Android handles transparency across views and drawables (including PNG images) natively, so the scenario you describe (a partially transparent ImageView in front of a Gallery) is certainly possible.

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

If you're having problems it may be related to either the layout or your image. I've replicated the layout you describe and successfully achieved the effect you're after. Here's the exact layout I used.

<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>

请注意,我已将父 RelativeLayout 更改为 fill_parent 的高度和宽度,这通常是您想要的主要活动.然后我将 ImageView 的顶部和底部与 Gallery 的顶部和底部对齐,以确保它在它的前面居中.

Note that I've changed the parent RelativeLayout to a height and width of fill_parent as is generally what you want for a main Activity. Then I've aligned the top and bottom of the ImageView to the top and bottom of the Gallery to ensure it's centered in front of it.

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

I've also explicitly set the background of the ImageView to be transparent.

至于图像drawable本身,如果你把PNG文件放在某个地方让我看,我可以在我的项目中使用它,看看它是否负责.

As for the image drawable itself, if you put the PNG file somewhere for me to look at I can use it in my project and see if it's responsible.

这篇关于Android 中的重叠视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:44