【问题标题】:Monodroid: using extended view class in xmlMonodroid:在 xml 中使用扩展视图类
【发布时间】:2013-01-10 13:44:55
【问题描述】:

我有一个扩展 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>

感谢您的帮助。

【问题讨论】:

    标签: android xml xamarin.android


    【解决方案1】:

    替换这个

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

    【讨论】:

    【解决方案2】:

    将您的布局 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

    【讨论】:

      【解决方案3】:

      感谢您的帮助,现在可以使用以下代码。包名有点混乱,因为在我使用的 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多