【发布时间】: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