自定义微调器视图

时间:2012-06-18 12:33:12

标签: android android-arrayadapter custom-attributes custom-component android-spinner

我创建了自己的自定义微调器,我可以在“自定义和库视图”中访问它,因为它实现了View。但是,问题是当从我的main.xml中将我的自定义微调器从调色板拖动到它时,它会抛出“未处理的事件循环异常”。我不知道我的错误在哪里。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pref="http://schemas.android.com/apk/res/com.myspinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.myspinner.Myspinner
android:id="@+id/myspinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
pref:MyEntries="@array/testarray" />

MySpinnerattributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MySpinner">
        <attr name="MyEntries" format="reference"/>
    </declare-styleable>
</resources>

spinnerlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:text="@string/large_text" android:gravity="center"/>

Myspinner.java

    public class Myspinner extends Spinner{


    LayoutInflater inflater;
    CharSequence cs[]={"MySpinner"};
    String s[]={"MySpinner"};
    View row;
    TextView txt;


public Myspinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setAttributes(attrs);    
    this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s));
    }



public Myspinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttributes(attrs);
    this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s));
}



public class MyAdapter extends ArrayAdapter<String>{


public MyAdapter(Context context, int textViewResourceId,String[] objects){
    super(context, textViewResourceId, objects);
      // TODO Auto-generated constructor stub
  }



  public View getDropDownView(int position, View convertView,ViewGroup parent){
    return getCustomView(position, convertView, parent);
  }



   public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
   }



    public View getCustomView(int position, View convertView, ViewGroup parent) {

    inflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row=inflater.inflate(R.layout.spinnerlayout, parent, false);
      txt=(TextView)row.findViewById(R.id.textView1);
      txt.setText(s[position]);
      return row;
    }
    }

       public void setAttributes(AttributeSet attrs){
       if (attrs != null) 
       {
       TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MySpinner);
       final int N = a.getIndexCount();
       for (int i = 0; i < N; ++i)
       {      
           int attr = a.getIndex(i);
           switch (attr)
           {
                 case R.styleable.MySpinner_MyEntries:
                 cs=a.getTextArray(attr);
                 s=new String[cs.length];
                 for (int j = 0; j < cs.length; ++j)
                 s[j]=(String)cs[j];
                 break;

                 default:break;
            }
        a.recycle();
        } 
            }
            }
            }

如果您只是复制粘贴main.xml代码,则不会出错。当我从调色板中拖动自定义微调器时,它会出现错误。

0 个答案:

没有答案