【问题标题】:android custom view not inflatingandroid自定义视图不膨胀
【发布时间】:2015-11-15 23:16:06
【问题描述】:

我制作了一个扩展 View 类的自定义视图:

 import android.content.Context;
 import android.graphics.Canvas;
 import android.view.View;

 public class DrawSurfaceView extends View
 {

private ButtonsManager BM;
public DrawSurfaceView(Context context) {
    super(context);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    BM.draw(canvas);
}


}

我将它包含在 xml 中:

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

        <TextureView
        android:id="@+id/gameSurface"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

                    <com.example.trashedometer.DrawSurfaceView
                android:id="@+id/drawingSurface"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                />

</FrameLayout>

但是在设置内容视图的主代码中,它说它不能为我的自定义视图充气,为什么?

 setContentView(R.layout.activity_main);

【问题讨论】:

    标签: android view layout-inflater


    【解决方案1】:

    您至少需要一个构造函数来允许从 XML 布局进行膨胀。

    public class DrawSurfaceView extends View {
        private ButtonsManager BM;
    
        public DrawSurfaceView(Context context) {
            this(context, null);
        }
    
        public DrawSurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        ...
    }
    

    【讨论】:

    • 我仍然遇到同样的问题。以上解决方案无效
    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多