How to use own view in layout?

2019-02-13 10:40发布

I created a class like this

public final class MyView extends View {

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        [...]
    }
        [...]
}

and then I want to use it within my layout.xml

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

  <com.hitziger.barcode.MyView
      android:id="@+id/my_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>

</FrameLayout>

But Eclipse tells me in the error log

AndroidManifest: Ignoring unknown 'com.hitziger.barcode.MyView' XML element

How can I make MyView accessable within a layout? Do I have to publish this class elsewhere?

2条回答
地球回转人心会变
2楼-- · 2019-02-13 11:08

You should write it like:

<view class="com.hitziger.barcode.MyView"...
查看更多
Evening l夕情丶
3楼-- · 2019-02-13 11:28

in the layout.xml, use:

<View 
    android:class="com.hitziger.barcode.MyView"
    android:id="@+id/my_view"
...

istead of:

<com.hitziger.barcode.MyView
    android:id="@+id/my_view"
查看更多
登录 后发表回答