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