如何将一个视图显示为另一个视图的叠加?
我有两个占据整个屏幕的视图,并且我想同时显示两个视图,一个在另一个之上。我的布局如下所示:
<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
的背景颜色更改为透明,但这没有什么区别。
我还希望能够将 myCustomView
作为 WebView
的叠加层,反之亦然。
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>
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.
I would also like to have the ability to make myCustomView
as an overlay on WebView
or vice versa.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先使用
RelativeLayout
。您可以使用
ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
或变体以及ViewGroup.removeView(View view)
或ViewGroup .removeViewAt(int索引)
。这显然需要您使用 LayoutInflater 手动膨胀视图,但您可以在膨胀后保留对每个视图的全局引用,然后在两者之间翻转。
Use a
RelativeLayout
for starters.You could use the
ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
or a variant along withViewGroup.removeView(View view)
orViewGroup.removeViewAt(int index)
.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.我不确定当它们位于同一个 xml 文件中时是否可以执行此操作,但我知道如果您移动:
到另一个 xml 文件,并创建另一个活动。将内容视图设置为包含 org.example.myCustomView 的 xml 文件并调用该活动。在清单中,当您添加该活动时,请添加一个主题语句,如下所示:
完整的语句应如下所示:
结果将如下所示(仅与您的组件一起使用):
尽管当它们仍在同一个 xml 文件中时也许可以这样做。如果可能的话,可能会通过单独保留清单并将您的costomeView编辑为以下内容来完成:(
我将主题语句添加到 CustomView 中)
如果这不是您想要的,那么我会推荐一些东西和Nick Campion说的类似,就是把fillParent改成wrapContent。
你可以这样做,或者你可以选择你想要的尺寸。您可以使用以下两个选项:(
您可以将“50dip”更改为您想要的任何数字,只要它以dip结尾即可)
或:
I'm not sure if you can do this when they are in the same xml file, but I know that if you move:
to another xml file, and create another activity. set the content view to the xml file that contains the org.example.myCustomView and call that activity. in the manifest, when you add that activity, add a theme statement as so:
the full statement should look like this:
the result will look like this (only it will be with your components instead):
although it mabey possible to do it when they are still in the same xml file. if it is possible, it would likely be done by leaving the manifest alone, and editing your costomeView to the following:
(I added the theme statement into the CustomView )
if this isn't what you are looking for, than I would recoment something simmilar to what Nick Campion said, which was to change fillParent to wrapContent.
you could do that, or you could choose what size you wanted it to be. here are two options you could use:
(you could change "50dip" to whatever number you want as long as it ends with dip)
or:
术语
fill_parent
表示消耗父级中所有允许的空间。尝试将其设置为wrap_content
。然后您可以尝试向两者添加weight = 1
以尝试均匀分配剩余空间。The term
fill_parent
means consume all the allowable space in the parent. Try setting it towrap_content
. Then you can try addingweight = 1
to both to try to evenly distribute the remaining space.