【问题标题】:Change Background colour of ListView in android at run time在运行时更改android中ListView的背景颜色
【发布时间】:2012-04-20 08:50:40
【问题描述】:

我正在 Android 中做项目。我想更改 ListView 中所选项目的 background 颜色和 textcolor。这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <ListView
            android:id="@+id/listView1"
            android:layout_width="265dp"
            android:layout_height="366dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:layout_weight="0.00"
            android:drawSelectorOnTop="true" >

        </ListView>
    </LinearLayout>

</LinearLayout> 

所以,我有 ListView 和一些学生姓名,并且可以使用 checkbox 进行多项选择。

ListView stud_lst=(ListView) findViewById(R.id.listView1);

stud_lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

我想更改所选学生的背景和文字颜色。 我已经看到了一些答案,但我没有得到它。 请帮帮我。

【问题讨论】:

  • 为listview制作自定义行。onclick改变自定义行的背景颜色和文本颜色。
  • 不使用自定义行的任何其他想法

标签: java android listview android-listview


【解决方案1】:

使用自定义适配器并在您的活动类中执行以下操作:

   //  mListview is ur listview object.
   mListview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                    view.setBackgroundColor("your bg's color id");
            }
   }

【讨论】:

    【解决方案2】:

    您必须创建一个自定义适配器来更改项目的背景颜色。这是自定义适配器的示例:

    public class PaListAdapter  extends BaseAdapter{
            private LayoutInflater mInflater;
    
             private ArrayList<String> platevalue = new ArrayList<String>();
    
               ViewHolder holder;
            public PaListAdapter(Context context,ArrayList<String> value)
            {
                // Cache the LayoutInflate to avoid asking for a new one each time.
                mInflater = LayoutInflater.from(context);
    
    
    
                //mycontext = context;
                platevalue.clear();
                platevalue =value;
    
    
    
            }
    
    
            public int getCount() 
            {
                return platevalue.size();
            }
    
            public Object getItem(int position) 
            {
                return position;
            }
    
            public long getItemId(int position) 
            {
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) 
            {
    
    
    
    
    
                if (convertView == null) 
                {
                     convertView = mInflater.inflate(R.layout.select_dialog, null);
    
                    holder = new ViewHolder();
                    holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);
    
    
    
    
                    convertView.setTag(holder);
                }
                else 
                {
                    holder = (ViewHolder) convertView.getTag();
                }
    
                holder.hTransID.setText(platevalue.get(position));
    
    
    
    
                return convertView;
            }
    
            static class ViewHolder 
            {      
                  TextView    hTransID;
    
    
            }
        }
    

    select_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:descendantFocusability="blocksDescendants"
     android:background="#000000"
        >
    
        <TextView
            android:id="@+id/txtChoice"
    
            android:layout_gravity="center_vertical|left"
            android:gravity="center_vertical|left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:textColor="#000000"/> 
    
    </LinearLayout>
    

    在活动类中。定义如下:

    simpleefficientadapter efficientadapter;
    
      efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
       listView.setAdapter(efficientadapter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      相关资源
      最近更新 更多