问题描述
在我项目的主类中,我有
In the main class of my project, I have
public class MyClass extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
...
}
class MySurfaceView extends SurfaceView implements Runnable{
...
}
}
我想为这个程序添加按钮和滚轮,我现在在一个单独的小程序中使用 main.xml 文件.我知道要访问它,我必须使用 setContentView(R.layout.main) 行,但是,我失去了对 SurfaceView 的访问权限.我如何做到这一点,以便我拥有两者?我在网上看到了一些关于使用 FrameLayout 添加几行的内容,但我不知道在哪里,我尝试将它粘贴在 main.xml 中的不同位置,但它只会导致我的整个程序中断..
I want to add buttons and wheels to this program, which I have in a separate mini-program right now with a main.xml file. I know that to access that, I have to have the line setContentView(R.layout.main), but then, I lose access to my SurfaceView. How do I make it so that I have both? I saw something online about adding a few lines with a FrameLayout but I can't figure out where, and I've tried sticking it in various places in my main.xml but it just causes my entire program to break..
目前我的 main.xml 看起来像这样:
Currently my main.xml looks like this:
<?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"
>
<Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="top|left"
android:paddingLeft="12dp"
android:paddingRight="12dp"
>
<kankan.wheel.widget.WheelView android:id="@+id/startHour"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="43dp"/>
<kankan.wheel.widget.WheelView android:id="@+id/startMins"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView android:id="@+id/theWordTo"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:text=" to "/>
</LinearLayout>
<kankan.wheel.widget.WheelView android:id="@+id/endHour"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="43dp"
/>
<kankan.wheel.widget.WheelView android:id="@+id/endMins"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
推荐答案
我想你想使用 FrameLayout.将您的 main.xml 更改为如下所示:
I think you want to use FrameLayout. Change your main.xml to look like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<kankan.current.app.MyClass.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="top|left"
android:paddingLeft="12dp"
android:paddingRight="12dp"
>
<kankan.wheel.widget.WheelView android:id="@+id/startHour"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="43dp"/>
<kankan.wheel.widget.WheelView android:id="@+id/startMins"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TextView android:id="@+id/theWordTo"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:text=" to "/>
</LinearLayout>
<kankan.wheel.widget.WheelView android:id="@+id/endHour"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="43dp"
/>
<kankan.wheel.widget.WheelView android:id="@+id/endMins"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
然后在活动中:
public class MyClass extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySurfaceView = (MySurfaceView) findViewById(R.id.surfaceView);
}
class MySurfaceView extends SurfaceView implements Runnable{
...
}
}
这篇关于如何在 Android 上使用带有 main.xml 的 SurfaceView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!