在布局XML中使用自定义SurfaceView

在布局XML中使用自定义SurfaceView

本文介绍了Android的 - 在布局XML中使用自定义SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有这个简单的布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

            < SurfaceView
                机器人:ID =@ + ID / surfaceView1
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =0dp
                机器人:layout_weight =1/>

            <的LinearLayout
                机器人:ID =@ + ID / linearLayout1
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                机器人:重力=中心>

                <按钮
                    机器人:ID =@ + ID /按钮1
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文本=按钮1/>
                <按钮
                    机器人:ID =@ + ID /按钮2
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文本=按钮2/>
            < / LinearLayout中>
< / LinearLayout中>
 

这符合和应用程序完美运行。我想用我自己的自定义SurfaceView取代通用SurfaceView:

 进口android.content.Context;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;

公共类PuzzleView扩展了SurfaceView实现SurfaceHolder.Callback {

    公共PuzzleView(上下文的背景下){
        超(上下文);
        // TODO自动生成构造函数存根
    }
    @覆盖
    公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
            INT高度){
        // TODO自动生成方法存根
    }
    @覆盖
    公共无效surfaceCreated(SurfaceHolder持有者){
        // TODO自动生成方法存根
    }
    @覆盖
    公共无效surfaceDestroyed(SurfaceHolder持有者){
        // TODO自动生成方法存根
    }
}
 

和使用,在布局的xml:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=垂直>

                < PuzzleView
                    机器人:ID =@ + ID / surfaceView1
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =0dp
                    机器人:layout_weight =1/>
。
。
。
 

一旦活动创建我得到一个异常:

 公共类MyActivity延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.game);
    }

10-29 19:56:25.921:E / AndroidRuntime(287):java.lang.RuntimeException的:无法启动的活动ComponentInfo {ybz.pack1 / ybz.pack1.MyActivity}:android.view.InflateException:二进制XML文件中的行# 7:错误充气类PuzzleView
 

这是不允许的。我找不到这样做的任何例子。

修改
其他然后下面的解决方案给定的,它也需要SurfaceView的所有构造函数添加到PuzzleView:

 公共PuzzleView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    // TODO自动生成构造函数存根
}

公共PuzzleView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    // TODO自动生成构造函数存根
}
 

解决方案

在你的XML布局你也有写包(在这里你声明类)是这样的:

 < com.your.package.here.PuzzleView
                    机器人:ID =@ + ID / surfaceView1
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =0dp
                    机器人:layout_weight =1/>
 

I have this simple layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

            <SurfaceView
                android:id="@+id/surfaceView1"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight = "1" />

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="button1" />
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="button2" />
            </LinearLayout>
</LinearLayout>

This complies and the application runs perfectly.I wanted to replace the generic SurfaceView with my own custom SurfaceView:

import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PuzzleView extends SurfaceView implements SurfaceHolder.Callback {

    public PuzzleView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
}

And use that in the layout xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

                <PuzzleView
                    android:id="@+id/surfaceView1"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight = "1" />
.
.
.

As soon as the activity is created I get an exception:

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
    }

10-29 19:56:25.921: E/AndroidRuntime(287): java.lang.RuntimeException: Unable to start activity ComponentInfo{ybz.pack1/ybz.pack1.MyActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class PuzzleView

Is this not allowed. I can't find any examples for doing this.

Edit:
Other then the solution given below, its also required to add all the constructors of SurfaceView to PuzzleView:

public PuzzleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public PuzzleView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
解决方案

In your xml layout you also have to write the package(where you declare your class) like this:

<com.your.package.here.PuzzleView
                    android:id="@+id/surfaceView1"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight = "1" />

这篇关于Android的 - 在布局XML中使用自定义SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:52