开发者

Custom view in xml layout

I've created my own view by creating a subclass of the SurfaceView class.

However I can't figure out how to add it from the xml layout file. My current main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Lin开发者_如何学编程earLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<View
    class="com.chainparticles.ChainView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
    />


</LinearLayout>

What have I missed?

Edit

More info

My view looks like this

package com.chainparticles;
public class ChainView extends SurfaceView implements SurfaceHolder.Callback {
    public ChainView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }
// Other stuff
}

And it works fine like this:

ChainView cview = new ChainView(this);
setContentView(cview);

But nothing happens when trying to use it from the xml.


You want:

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

    <com.chainparticles.ChainView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
     />
</LinearLayout>

Edit:

After seeing the rest of your code it's probably throwing because you can't call getHolder in the constructor while being inflated. Move that to View#onFinishInflate

So:

@Override
protected void onFinishInflate() {
    getHolder().addCallback(this);
}

If that doesn't work try putting that in an init function that you call in your Activitys onCreate after setContentView.

It was probably working before because when inflating from xml the constructor: View(Context, AttributeSet) is called instead of View(Context).


What you missed in your example was the tag name, it supposed to be "view" (first non-capital) not "View". Although you can put your class name as the tag name most of the time, it's impossible to do that if your class is inner class, because "$" symbol, which is used in Java to reference inner classes is restricted in XML tags. So, if you want to use inner class in your XML you should write 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"
>

    <view
      class="com.chainparticles.Foo$InnerClassChainView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
     />
</LinearLayout>

The thing is that both "view" and "View" tags exist in the schema. "View" tag (started with capital letter) will generate a View class, while "view" tag, when parsed, will examine the class attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜