我需要在android gridview中将图像设置为背景。我有使用图像视图加载图像的代码。以下代码是我的代码,请帮我解决这个问题。
public void onCreate(Bundle savedInstanceState) {
if(!isNetworkAvailbale()){
Toast.makeText(getApplicationContext(), "Internet Connection not available", Toast.LENGTH_SHORT).show();
finish();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d("taust","sds");
Toast.makeText(NuesHoundRSSActivity.this, "Item co" + position, Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
package com.nues.rss;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
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) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
// imageView.setTag(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
任何团体都知道如何将图像设置为网格项的背景?
答案 0 :(得分:1)
第一步是创建一个布局并在自定义适配器的getView方法中对其进行膨胀。
这是一个例子,
http://www.technotalkative.com/android-gridview-example/
http://android-vogue.blogspot.in/2011/06/custom-gridview-in-android-with.html。
答案 1 :(得分:1)
好的,如果你想为GridView设置一个背景图像(我的意思是整个View的背景),你只需要在Activity类中设置它:
GridView gridview = (GridView)findViewById(R.id.gridview);
gridview.setBackgroundResource(R.drawable.sampleImage);
或者在XML布局文件中:
android:background="@drawable/sampleImage"
相反,如果您要为GridView的所有项目/元素设置背景图像(所有项目的图像都相同),您只需在ImageAdapter中执行此操作:
private Integer mThumbIds = R.drawable.sampleImage;
...
imageView.setImageResource(mThumbIds);
请注意,在适配器内部,您甚至可以使用TextView而不是ImageView,这样您就可以同时拥有图像和文本:
public View getView(int position, View view, ViewGroup parent) {
TextView newView;
if ( view == null ) {
newView = new TextView( this.mContext );
} else {
newView = (TextView) view;
}
//Set the text:
newView.setText( "TEXT" ); // android:text
newView.setTextColor( Color.WHITE ); // android:textColor
newView.setTextSize(25); // android:textSize
newView.setTypeface( Typeface.DEFAULT_BOLD ); // android:textStyle
newView.setGravity( Gravity.CENTER ); // android:gravity
//Set backgorund image:
newView.setBackgroundResource( this.thumbIds ); // android:background
newView.setWidth(64);
newView.setHeight(64);
return newView;
}