android listview选中某一行,成选中状态颜色高亮显示,ListView的UI显示中常见问题及解决办法

把下面的XML文件保存成你自己命名的.xml文件(比如list_bg.xml),注意,这个文件相当于一个背景图片选择器,在系统使用时根据
ListView中的列表项的状态来使用相应的背景图片,什么情况使用什么图片我在下面都进行了说明。还有,你可以把它看成是一个图片来使用,放于
drawable目录下,配置背景属性android:background="@drawable/list_bg"就能达到你需要的目的了。
<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android

">
  <item android:state_window_focused="false" 
        android:drawable="@drawable/没有焦点时的图片背景" /> 
  <item android:state_focused="true" android:state_pressed="true" 
        android:drawable=
              "@drawable/非触摸模式下获得焦点并单击时的背景图片" /> 
  <item android:state_focused="false" android:state_pressed="true" 
        android:drawable="@drawable/触摸模式下单击时的背景图片" /> 
  <item android:state_selected="true" 
        android:drawable="@drawable/选中时的图片背景" /> 
  <item android:state_focused="true" 
        android:drawable="@drawable/获得焦点时的图片背景" /> 
</selector> 

----------------------------------------------------------------你可以看下源代码ListView列表项背景的默认实现SDK目录\tools\lib\res\default\drawable\list_selector_background.xml




目标:选中item,其字体设置为#3197FF,未选中的,其字体为#FFFFFF。其效果图如下:

android listview选中某一行,成选中状态颜色高亮显示,ListView的UI显示中常见问题及解决办法_第1张图片

与listvew设置选中行item背景图片一样,使用selector,不过这里的颜色设置,应该是在listview中的textview中设置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml version = "1.0" encoding = "utf-8" ?> 
< TableLayout
     xmlns:android = "http://schemas.android.com/apk/res/android"   
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     >
          < TableRow >
          < TextView
             android:id = "@+id/name_tv" 
             android:layout_height = "wrap_content"
             android:layout_width = "wrap_content"
             android:textSize = "25px"
             android:textColor = "@drawable/itemcolor"
          </ TextView >
          </ TableRow >
</ TableLayout >

同样,定义itemcolor.xml文件,修改选中行的字体颜色:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android"
<!-- 没有焦点时字体颜色 -->
< item
     android:state_selected = "false"
     android:color = "#FFFFFF" />
<!--选中时的字体颜色  --> 
< item
     android:state_selected = "true"
     android:color = "#3197FF" />
<!-- 非触摸模式下获得焦点并单击时的字体颜色 --> 
< item
     android:state_focused = "true"
     android:state_pressed = "true"
     android:color = "#3197FF" />
</ selector >
1
  

完成了。



屏蔽获取焦点时的高亮橙色显示:

listview.setSelector(R.drawable.nocolor);图片为一张无色透明图片即可,或者

android:listSelector="@drawable/nocolor"






构造自己的adapter,在getView中,设置颜色,点击item,刷新listview。

代码:

view plaincopy to clipboardprint?
import java.util.ArrayList;   
import java.util.HashMap;   
import java.util.List;   
import java.util.Map;   
import android.app.Activity;   
import android.content.Context;   
import android.graphics.Color;   
import android.os.Bundle;   
import android.view.LayoutInflater;   
import android.view.View;   
import android.view.ViewGroup;   
import android.widget.AdapterView;   
import android.widget.BaseAdapter;   
import android.widget.ListView;   
import android.widget.TextView;   
public class MyListView4 extends Activity {   
    private List<Map<String, Object>> mData;       
    ListView setlistViewLeft;   
    MyAdapter adapter;     
       
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.list);   
           
        mData = getData();         
        setlistViewLeft = (ListView)findViewById(R.id.listleft);    
           
        adapter = new MyAdapter(this);   
        setlistViewLeft.setAdapter(adapter);   
        setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);   
    }   
    private List<Map<String, Object>> getData() {   
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();   
        Map<String, Object> map = new HashMap<String, Object>();   
        map.put("title", "G1");   
        map.put("info", "google 1");   
        list.add(map);   
        map = new HashMap<String, Object>();   
        map.put("title", "G2");   
        map.put("info", "google 2");   
        list.add(map);   
        map = new HashMap<String, Object>();   
        map.put("title", "G3");   
        map.put("info", "google 3");   
        list.add(map);   
           
        map = new HashMap<String, Object>();   
        map.put("title", "G4");   
        map.put("info", "google 4");   
        list.add(map);   
           
        map = new HashMap<String, Object>();   
        map.put("title", "G5");   
        map.put("info", "google 5");   
        list.add(map);   
        return list;   
    }   
    AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {   
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {   
               
            adapter.setSelectItem(arg2);   
            adapter.notifyDataSetInvalidated();   
//          adapter.notifyDataSetChanged();   
        }   
           
    };   
    /**  
     * listview中点击按键弹出对话框  
     */  
    public final class ViewHolder {   
        public TextView titleText;   
        public TextView infoText;          
    }   
    public class MyAdapter extends BaseAdapter {   
        private LayoutInflater mInflater;   
        public MyAdapter(Context context) {   
            this.mInflater = LayoutInflater.from(context);   
        }   
        public int getCount() {   
            // TODO Auto-generated method stub   
            return mData.size();   
        }   
        public Object getItem(int arg0) {   
            // TODO Auto-generated method stub   
            return mData.get(arg0);   
        }   
        public long getItemId(int arg0) {   
            // TODO Auto-generated method stub   
            return arg0;   
        }   
        public View getView(int position, View convertView, ViewGroup parent) {   
            ViewHolder holder = null;   
            if (convertView == null) {   
                holder = new ViewHolder();   
                convertView = mInflater.inflate(R.layout.leftview, null);   
                holder.titleText = (TextView) convertView.findViewById(R.id.titleleftlist);   
                holder.infoText = (TextView) convertView.findViewById(R.id.infoleftlist);   
                   
                convertView.setTag(holder);            
            } else {   
                holder = (ViewHolder) convertView.getTag();   
            }   
               
            holder.titleText.setText((String) mData.get(position).get("title"));   
            holder.infoText.setText((String) mData.get(position).get("info"));             
               
            if (position == selectItem) {   
                convertView.setBackgroundColor(Color.RED);   
            }    
            else {   
                convertView.setBackgroundColor(Color.TRANSPARENT);   
            }      
               
            //convertView.getBackground().setAlpha(80);    
               
            return convertView;   
        }   
        public  void setSelectItem(int selectItem) {   
             this.selectItem = selectItem;   
        }   
        private int  selectItem=-1;   
    }   



.ListView的UI显示中常见问题及解决办法

1>如果需求是listview点击时,item无背景变色效果

步骤:

  1.drawable文件夹中新建timer_list_selector.xml内容如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true" android:drawable="@color/transparent"/>
</selector>

  2.values文件夹中新建colors.xml内容如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent">#50000000</color>
</resources>

  3.布局文件中给listview加入如下属性

android:listSelector="@drawable/timer_list_selector"

2>listview设置分割线Divider样式,布局文件给listview加入如下属性(已分隔符为颜色为例)

  android:divider属性用来设置分割线颜色(或图片),当设置为#00000000时表示item之间无间隙;

  通过android:dividerHeight属性设置分割线高度

    android:divider="#ff999999"
       android:dividerHeight="1sp"

3>UI展现时常见问题

  问题1:listview设置背景,拖动listview时显示黑色,只有拖动完才会显示我们设置的背景颜色或图片
  产生原因:listview的背景是固定不变的,默认Listview的每项的背景是透明的,拖动滚动条的过程中需要实时将每个项的显示内容跟背景进行混合运算,android系统为了优化这个过程,使用了android:cacheColorHint属性,在黑色背景下默认颜色为#191919,所以出现了上面的黑色显示问题
  解决办法:(根据需求而定)

    1.如果只换背景颜色:将android:cacheColorHint设置成和背景颜色一样或android:scrollingCache="false"如下   

      android:cacheColorHint="#ff00ff00"
      android:background="#ff00ff00"

      android:scrollingCache="false"
          android:background="#ff00ff00"

    2.如果用图片做背景:将android:cacheColorHint设为#00000000变为透明或android:scrollingCache="false"即可如下

      android:cacheColorHint="#00000000"
          android:background="@drawable/ic_launcher"

      android:scrollingCache="false"
          android:background="@drawable/ic_launcher"

  问题2:listview上面或下面有黑色阴影

  解决办法:布局文件中给listview加入android:fadingEdge="none"
  UI总结:

  综上问题得到最终的布局为(带分割线+背景色)
 

复制代码
<ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="180dip"
        android:divider="#ff999999"
        android:dividerHeight="1sp"
        android:fadingEdge="none" 
        android:scrollingCache="false"
        android:background="#ff00ff00"
        />

你可能感兴趣的:(android,object,ListView,Google,layout,encoding)