在SurfaceView上渲染按钮

时间:2011-04-29 18:32:28

标签: android surfaceview

我一直在谷歌上寻找如何做到这一点,从我收集的内容来看,它并不像123那么简单,因为表面视图只会在它上面绘制,人们所说的解决方案是使用xml布局然后制作一个布局,然后将表面视图添加为子视图,将按钮添加为子视图。但是我完全迷失了,我真的很想看到一些关于如何做到这一点的代码(我更喜欢看例子和试错法学习)。

我的xml文件

<?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>

我没有添加按钮的代码,除了你在xml上看到的内容(我一直在做很多试验和错误)。

项目的工作方式如下:activity - &gt; setContentView(表面视图) - &gt;在onDraw上渲染gfx

有人可以帮我解决这个问题,一旦我看到一个关于如何使用1个对象执行此操作的示例,我将能够使用其他对象。

编辑 - 解决了它 o.k我已经成功地从这篇文章http://www.mail-archive.com/android-beginners@googlegroups.com/msg11780.html

中解决了这个问题

我必须将内容视图设置为xml文件,并将一些构造添加到曲面视图中以便它可以工作

1 个答案:

答案 0 :(得分:0)

我必须做类似的事情才能在ImageView上显示SurfaceView,并按照以下方式执行:

<?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>

这会导致按钮浮动在右下角的SurfaceView顶部。