2012-01-21 25 views
2

我想要將DragnDrop listview從這裏的https://github.com/commonsguy/cwac-touchlist轉換爲C#與Mono for Android。MonoDroid如何在佈局axml文件中使用自定義屬性?

這個自定義視圖的一部分要求使用聲明一些自定義屬性的文件資源/價值/ attrs.xml內的以下內容:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<declare-styleable name="TouchListView"> 
    <attr name="normal_height" format="dimension" /> 
    <attr name="expanded_height" format="dimension" /> 
    <attr name="grabber" format="reference" /> 
    <attr name="dragndrop_background" format="color" /> 
    <attr name="remove_mode"> 
     <enum name="none" value="-1" /> 
     <enum name="fling" value="0" /> 
     <enum name="slide" value="1" /> 
     <enum name="slideRight" value="1" /> 
     <enum name="slideLeft" value="2" /> 
    </attr> 
</declare-styleable> 
</resources> 

然後我嘗試使用它們我的佈局文件中像這樣:

<app.monodroid.TouchListView xmlns:tlv="http://schemas.android.com/apk/res/app.monodroid"  
    android:id="@+id/lstExercises" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/lExerciseActions" 
    android:drawSelectorOnTop="false" 
    tlv:normal_height="64dip" 
    tlv:grabber="@+id/icon" 
    tlv:remove_mode="slideRight" 
    /> 

但是當我嘗試建立項目中,我得到了以下錯誤消息:

/Library/Frameworks/Mono.framework/External/xbuild/Novell/Novell.MonoDroid.Common.targets: Error: Tool exited with code: 1. Output: /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'normal_height' in package 'com.app.monodroid' /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'grabber' in package 'com.app.monodroid' /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'remove_mode' in package 'com.app.monodroid' (App.MonoDroid)

我的項目名稱是App.MonoDroid。

如何在佈局文件中使用這些屬性?

回答

3

如果您爲應用程序聲明瞭包名,這些錯誤應該消失。在項目屬性,進入到Android清單標籤,你會看到包名稱的文本字段:

Setting the package name

0

...然後確保你的裝配和命名空間使用這個包名:

monodroid config

,並在XML文件中使用它:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res/**app.monodroid**"    
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <**app.monodroid**.FICalendarView 
    android:id="@+id/FICalendar" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    custom:dayStyle="1"> 
    </**app.monodroid**.FICalendarView> 
    <Button 
    android:id="@+id/MyButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Hello"/> 
</LinearLayout> 

其中FICaldendarView是自定義視圖定義爲:

namespace app.monodroid 
{ 
    public class FICalendarView : View 
    { 
     //private FICalenderView() {} 

     public FICalendarView(Context context) 
      : base(context) 
     { 

     } 

     public FICalendarView(Context context, Android.Util.IAttributeSet attribute) 
      : base(context, attribute) 
     { 
      Android.Content.Res.TypedArray a = context.Theme.ObtainStyledAttributes(attribute, Resource.Styleable.FICalendarView, 0, 0); 

      //Android.Util.TypedValue typedValue = null; 

      int dayStyle = a.GetInteger(Resource.Styleable.FICalendarView_dayStyle,0); 

     } 

     public FICalendarView(Context context, Android.Util.IAttributeSet attribute, int x) 
      : base(context, attribute,x) 
     { 

     } 
    } 
} 
相關問題