zoukankan      html  css  js  c++  java
  • 【Android】listview选中行字体变大

    目标:listview中item使用textview,当item选中时,字体为25px;当item未选中时,字体21px

    device

    之前想了很久,以为同listview选中行字体颜色一样,使用xml文件中使用selector就可以改变了,但是一直上网查找资料,都没有找到selector中改变字体大小的命令。后来网友提醒我,可以在adapter中的getview中修改。现将重要的代码放上:

    //全局变量,记录选中的item 
    public static int select_item = -1;

    先使用全局变量记录选中的item,然后在listview的OnItemSelectedListener中实时更新选中的item,并且使用adapter.notifyDataSetChanged()刷新listview数据。

    listview_listMenu.setOnItemSelectedListener(new OnItemSelectedListener(){
    	@Override
    	public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
    	select_item = arg2;	//当前选择的节目item
    
    	listAdapter.notifyDataSetChanged();	//通知adapter刷新数据
    	}
    
    	public void onNothingSelected(AdapterView<?> arg0) {
    	}
    });

    接着在adapter的getview中修改字体大小。

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) 
    	{
    		convertView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.menulistitem, null);
    		TextView listItem = (TextView)convertView.findViewById(R.id.name_menu);
    		listItem.setText(list.get(position).get("name_menu").toString());
    		
    		this.select_item = LiveChannelsActivity.select_item;
    	
    		try{
    			if(this.select_item == position){
    				listItem.setTextSize(25);	//选中的Item字体:25px
    			}
    			else
    				listItem.setTextSize(21);	//未选中的Item字体:21px
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    
    		
    		return convertView;
    	}

    这样就可以了。

  • 相关阅读:
    CloudStack 4.2 与CloudStack 4.1二级存储API发生变化
    添加虚拟机磁盘扩容步骤
    NAT概述
    CloudStack全局参数
    在 Web 项目中应用 Apache Shiro
    使用 Spring Security 保护 Web 应用的安全
    获取浏览器的homepage
    剑指offer系列——2.替换空格
    剑指offer系列——1.二维数组中的查找
    JDK下载需要Oracle账号登录问题
  • 原文地址:https://www.cnblogs.com/Amandaliu/p/2080347.html
Copyright © 2011-2022 走看看