0
我正在使用GridView和图像选择器,以便在按下按钮时与未按下按钮时图像不同。一切都在编译,但是当我运行应用程序时,出现错误“Binary XML file line#9:Error inflate class selector”。在GridView中使用图像选择器时膨胀类选择器时出错
是否可以在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;
}
}
谢谢,我这样做,并添加了一些额外的空间,图像的底部,它的工作。我还必须在文本的开头添加一些额外的空格,以便使其居中对齐,即使我指定了center_horizontal。如果你知道一个更好的方法来做到这一点,然后添加空间让我知道。 – Andy 2012-08-07 19:44:23
那么你的TextView宽度说,“wrap_content”这意味着居中它将不会做任何事情,因为你的TextView将总是(基本上)与你的文本相同的大小。你需要将它设置为“fill_parent”或“match_parent”(取决于api),以便让它做你想做的事。 – 2012-08-07 19:49:24