0

我正在使用 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;

}


}
4

1 回答 1

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" >
于 2012-08-07T18:46:52.467 回答