Border for gridview items in android
I displayed image and text in a gridview. i.e. Each item consi开发者_JS百科sts of one image and one text. It works fine. But I would like to know how to set the border of each gridview item separately in android.
1) create a attrs.xml under res>value folder.
2) Add a resource :
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
3) Add following code in your respective activity:
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
int mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
4) then set the mGalleryItemBackground
as the background of your view. You will get a border outside your view.
For example:
imageView.setBackgroundResource(mGalleryItemBackground);
You can also create a customed background by creating your own shape file...
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
and then simply add a background to the resource...
imageView.setBackgroundResource(R.drawable.shape);
精彩评论