我如何将一个视图显示为另一个视图的叠加? [英] How would I display one view as an overlay of another?
问题描述
我有两个视图占据整个屏幕,我想同时显示两个视图,一个在另一个之上.我的布局如下所示:
I have two views that take the whole screen and I want to display both views at the same time, one on top of the other. My layout looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<org.example.myCustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
注意myCustomView
使用onDraw(这个方法最后一个语句是invalidate())来绘制自定义图形.我遇到的问题是它只显示 myCustomView
,WebView
是隐藏的.我尝试将 mycustomView
的背景颜色更改为透明,但这没有区别.
Note that myCustomView
uses onDraw (this method last statement is invalidate()) to draw custom graphics. The problem I am getting is that it only displays myCustomView
, WebView
is hidden. I tried to change the background colour of mycustomView
to transparent but this makes no difference.
我还希望能够将 myCustomView
作为 WebView
的叠加层,反之亦然.
I would also like to have the ability to make myCustomView
as an overlay on WebView
or vice versa.
推荐答案
对初学者使用 RelativeLayout
.
您可以使用 ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
或与 ViewGroup.removeView(View view)
或 <代码>ViewGroup.removeViewAt(int index).
You could use the ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
or a variant along with ViewGroup.removeView(View view)
or ViewGroup.removeViewAt(int index)
.
这显然需要您使用 LayoutInflater
手动膨胀视图,但您可以在膨胀后保留对每个视图的全局引用,然后在两者之间切换.
This would obviously require you to inflate the views manually using LayoutInflater
but you could keep a global reference to each after inflating and just flip between the two.
这篇关于我如何将一个视图显示为另一个视图的叠加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!