我读起的Android 4.0开发和第5章就谈到画廊和ImageVievs并介绍了申报,设置样式 XML标签没有解释它的目的..我试图找到一些信息也在参考,没有运气。例如,我们有以下几点:
RES /值/ attrs.xml
<?xml version=”1.0” encoding=”utf-8”?>
<resources>
<declare-styleable name=”Gallery1”>
<attr name=”android:galleryItemBackground” />
</declare-styleable>
</resources>
example.java
public class GalleryActivity extends Activity {
[...]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
[...]
}
[...]
public class ImageAdapter extends BaseAdapter {
[...]
int itemBackground;
public ImageAdapter(Context c) {
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(
R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
[...]
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
我读过的代码几次,我真的不明白有一个ATTR孩子只能用name属性定义该设置样式Gallery1的目的..你能帮助我吗? 这是galleryItemBackground什么系统提供的或者是由我们定义的东西吗? 什么是我们在这段代码在做什么?
预先感谢您的任何帮助!