Rendering a button over SurfaceView

坚强是说给别人听的谎言 提交于 2019-12-11 06:25:11

问题


I've been looking on google on how to do this, from what I've gathered it's not as easy as 123 because the surface view will just draw above it, the solution which people have said is to use an xml layout and make a layout and then add the surface view as a child and the button as a child to that. However I'm completely lost on this I'd really love to see some code on how to do this (I prefer looking at examples and trial and error for learning).

My xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<FrameLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button
 android:id="@+id/Menu"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Menu" />
<Android.Game.GameView
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>

I've got no code for adding in the button except for what you see on the xml (I've been doing a lot of trial and error).

project works like this: activity --> setContentView(surface view) --> render the gfx on the onDraw

Can someone please help me with this, once I've seen an example on how to do this with 1 object I'll be able to do it with other objects.

EDIT - solved it o.k I've managed to work it out from this post http://www.mail-archive.com/android-beginners@googlegroups.com/msg11780.html

I had to set the content view to the xml file, and add some constructs to the surface view so it would work


回答1:


I had to do something similar to display an ImageView over-top of a SurfaceView, and did it as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <com.my.package.DrawingCanvas
    android:id="@+id/canvas"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:background="#ffffff" />
    <RelativeLayout
        android:id="@+id/toolbar_notification"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/toolbar_notification_icon"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/content_desc_save_icon"
            android:gravity="center|center_vertical" />
    </RelativeLayout>
</RelativeLayout>

This results in the button floating over top of the SurfaceView on the bottom-right corner.



来源:https://stackoverflow.com/questions/5835913/rendering-a-button-over-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!