在GridView中使用图像选择器时出错类扩展器时出错

时间:2012-08-07 17:55:27

标签: android gridview selector

我正在使用GridView和一个图像选择器,这样当我按下时,我的图像与未按下时的图像不同。所有东西都编译,但是当我运行应用程序时,我得到一个错误“二进制XML文件行#9:错误膨胀类选择器”。

是否可以在gridview中使用图像选择器?当我从xml中取出选择器时,它运行正常。

这是每个网格项的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<selector   
     xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/status_button_down"
          android:state_pressed="true" />
    <item android:drawable="@drawable/status_button_down"
          android:state_focused="true" />
    <item android:drawable="@drawable/status_button_up" />
</selector>
<TextView
    android:id="@+id/grid_item_label"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Status"    
        android:textSize="9pt"
        android:typeface="sans"
        android:textColor="#000000"  
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal|bottom"

         />
</LinearLayout>

以下是网格视图适配器的代码:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    //return mThumbIds.length;
    return 16;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        //ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes

        gridView = new View(mContext);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.drawable.status_button, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);

        textView.setText("Status");


    } else {
        //imageView = (ImageView) convertView;
        gridView = (View) convertView;

    }

    return gridView;

}


}

1 个答案:

答案 0 :(得分:1)

选择器实际上应该在“drawables”文件夹中设置为自己的.xml文件。然后你可以这样称呼它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/my_selector" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >