我从DB检索图像+文本并将其显示在gridview.i.e中。 gridview的每个项目由一个图像+一个text.It工作正常。但我只想知道如何在android中分别设置每个gridview项目的边框。我怎么能这样做?
我的代码是..
public class HomePage extends Activity {
private ArrayList<SingleElementDetails> allElementDetails=new ArrayList<SingleElementDetails>();
DBAdapter db=new DBAdapter(this);
String category, description;
String data;
String data1;
GridView gridview;
Button menu;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
menu=(Button)findViewById(R.id.menus);
menu.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
gridview=(GridView)findViewById(R.id.gridview);
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
int mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
allElementDetails.clear();
db.open();
long id;
//id=db1.insertTitle1(category, description,r_photo);
Cursor cursor = db.getAllTitles1();
while (cursor.moveToNext())
{
SingleElementDetails single=new SingleElementDetails();
single.setDishName(cursor.getString(1));
single.setCateogry(cursor.getString(2));
single.setDescription(cursor.getString(3));
single.setImage(cursor.getBlob(4));
allElementDetails.add(single);
}
db.close();
CustomListAdapter adapter=new CustomListAdapter(HomePage.this,allElementDetails);
gridview.setAdapter(adapter);
gridview.setBackgroundResource(mGalleryItemBackground);
}
});
}
}
我在这里使用CustomListAdapter存储来自DB的检索数据。 我的CustomListAdapter代码是......
public class CustomListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private LayoutInflater mInflater;
public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
allElementDetails = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.listview1, null);
ImageView imageview = (ImageView) convertView.findViewById(R.id.image);
TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry);
TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry);
TextView textview3=(TextView)convertView.findViewById(R.id.description_entry);
textview1.setText(allElementDetails.get(position).getDishName());
textview2.setText(allElementDetails.get(position).getCategory());
if(allElementDetails.get(position).getDescription().length()>8)
textview3.setText(allElementDetails.get(position).getDescription().substring(0,8)+"...");
else
textview3.setText(allElementDetails.get(position).getDescription());
byte[] byteimage=allElementDetails.get(position).getImage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(byteimage);
BitmapFactory.Options op=new BitmapFactory.Options();
op.inSampleSize=12;
Bitmap theImage= BitmapFactory.decodeStream(imageStream,null,op);
imageview.setImageBitmap(theImage);
return convertView;
}
}
答案 0 :(得分:1)
也许有一种更简单的方法,但可能会创建一个xml作为drawables中的drawable,内部定义的层就像这样:
每个drawable由单个元素内的元素表示。
文件位置:
res/drawable/filename.xml
文件名用作资源ID。
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:src="@drawable/rectangle" />
</item>
<item>
<bitmap
android:src="@drawable/griditem" />
</item>
<item>
<nine-patch> other etc...
</item>
<item> other drawable etc.</item>
</layer-list>
可以将它用作drawable或bitmap,如下所示:
Bitmap bitmap=BitmapFactory.decodeResource(getApplicationContext.getResources(),R.drawable.filename);
一些文档here