1

我有一个扩展 Horizo​​ntalScrollView 的类。但我不知道如何在 xml 中使用它。如果我使用我的类名作为 xml 标记,我得到一个错误:Error inflating class MyClass

我的代码:

public class ToothScroller : HorizontalScrollView
{

    private GestureDetector mGestureDetector;

    public ToothScroller(Context context) : base(context)
    {
    }

    public ToothScroller(Context context, IAttributeSet attrs) : base(context, attrs)
    {

        SetFadingEdgeLength(0);
    }

    public ToothScroller(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
    }

    public ToothScroller(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }


    public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
    {
        Console.WriteLine(this.ScrollX);

        return base.OnInterceptTouchEvent (ev);
    }

}

还有我的 xml (axml) 代码:

    <MyClass
        android:id="@+id/scrollMyClass"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageSomething"
        android:fillViewport="true"
        android:background="@drawable/pusherbg">
        <RelativeLayout
            android:id="@+id/layoutSomething"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </MyClass>

谢谢你的帮助。

4

3 回答 3

2

替换这个

 <MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg"> 

  <YourPackage.YourClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">
于 2013-01-10T13:47:26.073 回答
1

将您的布​​局 xml 更改为:

<com.username.package.MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">
    <RelativeLayout
        android:id="@+id/layoutSomething"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</com.username.package.MyClass>

这是一个很好的资源,其中包含更多直接来自 Google 的详细信息,可能会有更多关于如何创建自定义视图的信息:http: //developer.android.com/guide/topics/ui/custom-components.html

于 2013-01-10T13:47:59.147 回答
0

感谢您的帮助,它现在可以使用以下代码。Packagename 有点令人困惑,因为在我使用的 Monodroid 中,它是我必须使用的命名空间,而不是包名。

<MyNamespace.MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">
    <RelativeLayout
        android:id="@+id/layoutSomething"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</MyNamespace.MyClass>
于 2013-01-10T14:08:50.890 回答