我想View在 Android 上创建一个自定义。我试图尽可能简单地做到这一点,并创建了一个几乎空的类MyView并在我的中使用它,LinearLayout但应用程序在“强制关闭”开始时失败。我怎样才能做一个简单的定制View?根据构建自定义组件,View如果我不覆盖,则大小为 100x100 onMeasure()。
public class MyView extends View {
public MyView(Context context) {
super(context);
}
}
我将它用于LinearLayout:
<view
class="com.example.MyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
我究竟做错了什么?
如果我使用item建议的构造函数和对超类的相应调用。然后“强制关闭”消失了,但我LinearLayout的坏了,之后的组件MyView没有显示。
这是我的main.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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:background="#f00"
android:text="Hello"
/>
<view
class="com.example.MyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:background="#00f"
android:text="World"
/>
</LinearLayout>