Android:设置列表视图文本的背景

时间:2013-12-09 16:09:19

标签: android android-layout listview colors background

我需要为this.之类的行创建一些文本的背景。如何更改代码以实现此目的?

我是否需要为列表视图扩展另一个基本适配器,如果是,我该怎么办呢?

代码:

 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>

2 个答案:

答案 0 :(得分:1)

将grid.xml更改为包括以下内容:

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#BBBBBB" />

答案 1 :(得分:0)

没有理由构建新的适配器。如果我误解了你想要做的事情,请告诉我,我会尽力帮助。

有两种方法可以做到这一点。一个xml,一个java:

Inside Grid window.xml

  <ListView android:id="@+id/grid_list_view" 
          android:background="#0000ff"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
/>

或通过代码:

  lv = (ListView) findViewById(R.id.grid_list_view); //lv is global
  lv..setBackgroundColor(Color.parseColor("#0000ff"));
  lv.setAdapter(simpleAdpt);