我需要爲某些文本ir行創建背景,如this.如何更改代碼以實現此目的?Android:設置列表視圖文本的背景
我是否需要爲列表視圖擴展另一個基礎適配器?如果有,我該如何做?
代碼:
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.grid_layout, null);
}else{
grid = (View)convertView;
}
ImageView imageView = (ImageView)grid.findViewById(R.id.gridimage);
TextView textView = (TextView)grid.findViewById(R.id.gridtext);
imageView.setImageResource(mThumbIds[position]);
textView.setText(mStringIds[position]);
return grid;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
lv = (ListView) findViewById(R.id.grid_list_view); //lv is global
lv.setAdapter(simpleAdpt);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent sam = new Intent(CategoriesActivity.this, CategoriesActivity.class);
startActivity(sam);
System.out.println("on item click");
}
});
public void makeList(String value) {
String prekes_pav = null;
int prekes_kiekis = 0;
switch (position) {
case 0:
prekes_pav = "fr_veg";
break;
case 1:
prekes_pav = "meat_fish";
break;
default:
prekes_pav = "No name";
}
if(listas.listContain("fr_veg"))
{
///////// MAKE text background BLUE
}
listas.createItem(prekes_pav, value, null);
simpleAdpt = new SimpleAdapter(this, listas.getList(), android.R.layout.simple_list_item_1, new String[] {prekes_pav}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
}
電網的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/gridimage"
android:layout_width="fill_parent"
android:layout_height="40dp"/>
<TextView
android:id="@+id/gridtext"
android:textColor="#FFFFFF"
android:textSize="16dp"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
網格窗口XML
<GridView
android:id="@+id/grid"
android:paddingTop="20dp"
android:layout_width="fill_parent"
android:layout_height="265dp"
android:columnWidth="50dp"
android:numColumns="4"
android:verticalSpacing="15dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="@drawable/bg_blue_gradient"
/>
<Button
android:id="@+id/cat_button_slide"
android:layout_height="wrap_content"
android:layout_width="60dp"
android:layout_gravity="center"
android:background="@drawable/tongue_down"
android:onClick="selfDestruct" />
<ListView android:id="@+id/grid_list_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
你想改變你的xml屬性是什麼? –
grid_list_view行的文本 – user3082220