Android 中的重叠视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/961944/
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
Overlapping Views in Android
提问by Alexander Stolz
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.
在 Android 中是否可以有重叠的视图?我想要一个 ImageView,前面有一个透明的 png,背景中有另一个视图。
edit:
编辑:
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.
这就是我目前所拥有的,问题是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>
edit:
编辑:
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>
to this
对此
<item name="android:background">#00000000</item>
采纳答案by Reto Meier
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.
Android 本机处理跨视图和可绘制对象(包括 PNG 图像)的透明度,因此您描述的场景( aImageView
前面的部分透明Gallery
)当然是可能的。
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>
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.
请注意,我已将父级RelativeLayout
更改为高度和宽度,fill_parent
这通常是您想要的主要活动。然后我对准顶部和底部的ImageView
顶部和底部Gallery
,以确保它集中在它的前面。
I've also explicitly set the background of the ImageView
to be transparent.
我还明确地将 的背景设置ImageView
为透明。
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.
至于图像drawable本身,如果你把PNG文件放在某个地方让我看,我可以在我的项目中使用它,看看它是否负责。
回答by reflog
Also, take a look at FrameLayout
, that's how the Camera's Gallery application implements the Zoom buttons overlay.
另外,看看FrameLayout
,这是相机的画廊应用程序实现缩放按钮覆盖的方式。
回答by Prasanta
If you want to add you custom Overlay screen on Layout, you can create a Custom Linear Layout and get control of drawing and key events. You can my tutorial- Overlay on Android Layout- http://prasanta-paul.blogspot.com/2010/08/overlay-on-android-layout.html
如果您想在布局上添加自定义叠加屏幕,您可以创建自定义线性布局并控制绘图和关键事件。你可以在我的教程 - Android 布局上的叠加 - http://prasanta-paul.blogspot.com/2010/08/overlay-on-android-layout.html
回答by Fred Grott
Visible gallery changes visibility which is how you get the gallery over other view overlap. the Home sample app has some good examples of this technique.
可见画廊改变了可见性,这就是你如何让画廊超越其他视图重叠。Home 示例应用程序有一些关于这种技术的很好的例子。
回答by алекс кей
The simples way arround is to put -40dp margin at the buttom of the top imageview
最简单的方法是在顶部图像视图的底部放置 -40dp 边距
回答by Prashast
Yes, that is possible. The challenge, however, is to do their layout properly. The easiest way to do it would be to have an AbsoluteLayout and then put the two images where you want them to be. You don't need to do anything special for the transparent png except having it added later to the layout.
是的,这是可能的。然而,挑战是正确地进行布局。最简单的方法是拥有一个 AbsoluteLayout,然后将两个图像放在您想要的位置。除了稍后将其添加到布局之外,您不需要为透明 png 做任何特殊的事情。