如何将背景设置为Android Gridview?

问题描述:

我需要设置图像作为android gridview中的背景。我有使用图像视图来加载图像的代码。以下代码是我的代码,请帮我解决这个问题。如何将背景设置为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

和究竟是什么问题? –

+0

老兄我的问题是,我需要将mThumbIds数组图像中的图像设置为网格图块中的背景,而不是图像视图,因为我需要将一些内容放入网格图块中。你知道该怎么做吗? –

+0

您是否尝试将其设置在'gridview.xml'中? –

是的,我明白了。您希望显示一个Custom GridView,并为每个网格元素设置背景,并在其上添加一些内容。

第一步是创建一个布局,并在自定义适配器的getView方法中使其膨胀。

这里是一个例子,

http://www.technotalkative.com/android-gridview-example/

http://android-vogue.blogspot.in/2011/06/custom-gridview-in-android-with.html

好吧,如果你想设置一个背景图片为GridView(整个景观的背景下,我的意思),你只需要设置在你的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); 

需要注意的是,你的适配器里面,你甚至可以用它代替了ImageView的一个TextView,这样就可以有图像和文字一起:

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; 
}