无法为GridView布局设置背景颜色

时间:2015-10-27 12:00:09

标签: java android xml gridview layout

我从未在Android上使用过GridView。我尝试在GridView中扩充我的布局,我希望每个布局都有不同的背景。我不能分别为每个布局设置背景颜色,每次我得到所有布局的灰色。

我的XML代码:

<RelativeLayout
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:id="@+id/back">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:layout_marginTop="10dp"
        android:src="@drawable/splashscreen"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Science"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:id="@+id/categoryTextView"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

网格适配器:

public class CellAdapter extends BaseAdapter {
private Context context;
private LinkedList<CellGrid> list;

public CellAdapter(Context context, LinkedList<CellGrid> list){
    this.context = context;
    this.list = list;
}


@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (View) inflater.inflate(
                R.layout.grid_cell_layout, null);
    }

    convertView.setBackgroundColor(list.get(position).getBackground());
    TextView categoryTextView = (TextView)convertView.findViewById(R.id.categoryTextView);
    ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
    categoryTextView.setText(list.get(position).getText());
    imageView.setImageResource(list.get(position).getImageId());
    RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
    Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
    return convertView;
}

}

5 个答案:

答案 0 :(得分:1)

如果不满足任何特定颜色,则可以将随机bg设置为每个视图:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// generate the random integers for r, g and b value
    int r = rand.nextInt(255);
    int g = rand.nextInt(255);
    int b = rand.nextInt(255);

    int randomColor = Color.rgb(r,g,b);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (View) inflater.inflate(
                R.layout.grid_cell_layout, null);

 convertView.setBackgroundColor(randomColor);
    }

答案 1 :(得分:0)

请尝试使用以下对您有帮助的代码。

使用以下代码替换适配器代码

@Override
public int getCount() {
   return list.size();
}

@Override 
public Object getItem(int position) {
     return list.get(position);
}

@Override
public long getItemId(int position) {
   return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = (View) inflater.inflate(
            R.layout.grid_cell_layout, null);
}
    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),       rnd.nextInt(256)); 
    convertView.setBackgroundColor(color);
    TextView categoryTextView =    (TextView)convertView.findViewById(R.id.categoryTextView);
    ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
    categoryTextView.setText(list.get(position).getText());
    imageView.setImageResource(list.get(position).getImageId());
    RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
    Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
    return convertView;
}
}

如果您有任何问题,请随时发表评论

答案 2 :(得分:0)

使用resources().getColor()从列表中获取颜色并将其设置为背景

relativelayout.setBackgroundColor(getResources().getColor(list.get(position).getBackground()));

答案 3 :(得分:0)

  

这是我的示例代码..

**<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:background="@color/dark_gray" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="13dp" />

**

答案 4 :(得分:0)

你设置颜色(AARRGGBB int值)还是颜色资源(R.color。*)?

convertView.setBackgroundColor(list.get(position).getBackground());

AARRGGBB格式应该有颜色。 要从资源中获取价值,请使用:

context.getResources().getColor(R.color.your_color);
相关问题