如何将一个视图显示为另一个视图的叠加?

发布于 2024-10-16 07:24:52 字数 917 浏览 3 评论 0原文

我有两个占据整个屏幕的视图,并且我想同时显示两个视图,一个在另一个之上。我的布局如下所示:

<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())来绘制自定义图形。我遇到的问题是它只显示 myCustomViewWebView 被隐藏。我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟的代价 2024-10-23 07:24:52

首先使用 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 with ViewGroup.removeView(View view) or ViewGroup.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.

终止放荡 2024-10-23 07:24:52

我不确定当它们位于同一个 xml 文件中时是否可以执行此操作,但我知道如果您移动:

<org.example.myCustomView
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent" />

到另一个 xml 文件,并创建另一个活动。将内容视图设置为包含 org.example.myCustomView 的 xml 文件并调用该活动。在清单中,当您添加该活动时,请添加一个主题语句,如下所示:

android:theme="@android:style/Theme.Dialog"

完整的语句应如下所示:

<activity android:name=".name_of_activity"  
    android:label="TextToBeOnTop"  
    android:theme="@android:style/Theme.Dialog">  
</activity>

结果将如下所示(仅与您的组件一起使用):

在此处输入图像描述

尽管当它们仍在同一个 xml 文件中时也许可以这样做。如果可能的话,可能会通过单独保留清单并将您的costomeView编辑为以下内容来完成:(

<org.example.myCustomView 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:theme="@android:style/Theme.Dialog" />

我将主题语句添加到 CustomView 中)

如果这不是您想要的,那么我会推荐一些东西和Nick Campion说的类似,就是把fillParent改成wrapContent。
你可以这样做,或者你可以选择你想要的尺寸。您可以使用以下两个选项:(

<org.example.myCustomView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="50dip"  
    android:layout_height="50dip" /> 

您可以将“50dip”更改为您想要的任何数字,只要它以dip结尾即可)

或:

<org.example.myCustomView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" />

I'm not sure if you can do this when they are in the same xml file, but I know that if you move:

<org.example.myCustomView
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent" />

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:

android:theme="@android:style/Theme.Dialog"

the full statement should look like this:

<activity android:name=".name_of_activity"  
    android:label="TextToBeOnTop"  
    android:theme="@android:style/Theme.Dialog">  
</activity>

the result will look like this (only it will be with your components instead):

enter image description here

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:

<org.example.myCustomView 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:theme="@android:style/Theme.Dialog" />

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

<org.example.myCustomView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="50dip"  
    android:layout_height="50dip" /> 

(you could change "50dip" to whatever number you want as long as it ends with dip)

or:

<org.example.myCustomView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" />
蓝咒 2024-10-23 07:24:52

术语 fill_parent 表示消耗父级中所有允许的空间。尝试将其设置为wrap_content。然后您可以尝试向两者添加 weight = 1 以尝试均匀分配剩余空间。

The term fill_parent means consume all the allowable space in the parent. Try setting it to wrap_content. Then you can try adding weight = 1 to both to try to evenly distribute the remaining space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文