【问题标题】:Can't set background color for GridView layouts无法为 GridView 布局设置背景颜色
【发布时间】:2016-01-26 20:00:44
【问题描述】:

我从未在 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;
}

}

【问题讨论】:

  • 什么是getBackground Returns,十六进制代码?
  • 它是 R.color.mycolor ,来自 color.xml,一个整数

标签: java android xml gridview layout


【解决方案1】:

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

用下面的代码替换你的适配器代码

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

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

【讨论】:

  • 我想从 colors.xml 文件中获取颜色,我该怎么做?谢谢
  • getResources().getColor(list.get(position).getBackground())
【解决方案2】:

如果任何特定颜色不满意,那么您可以将随机 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);
    }

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      这是我的示例代码..

      **<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" />
      

      **

      【讨论】:

        【解决方案5】:

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

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

        AARRGGBB 格式应该有颜色。 从资源使用中获得价值:

        context.getResources().getColor(R.color.your_color);
        

        【讨论】:

          猜你喜欢
          • 2015-02-14
          • 2011-11-14
          • 2012-06-04
          • 2014-03-29
          • 2020-12-08
          • 2013-01-07
          • 2017-08-31
          • 1970-01-01
          相关资源
          最近更新 更多