动态修改ListView文字大小

来源:互联网 发布:装修效果图 设计软件 编辑:程序博客网 时间:2024/05/16 12:11

写了一个MyAdapter(继承了BaseAdapter)去实现数据和UI的显示。通过滑动SeekBar来改变MyAdapter的getView方法中View的字体大小进而实现了改变ListView中文字大小的效果。


效果图:

          


1 MainActivity的布局文件main.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ListView   
  8.         android:id="@+id/listview01"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         ></ListView>  
  12.     <SeekBar   
  13.         android:id="@+id/seekbar01"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_alignParentBottom="true"  
  17.         />  
  18. </RelativeLayout>  

2 ListView的item布局文件main_listview01_item.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:id="@+id/textView01"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:textSize="23sp"  
  12.         />  
  13.     <TextView   
  14.         android:id="@+id/textView02"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:textSize="15sp"  
  18.         />  
  19.   
  20. </LinearLayout>  

3 MyAdapter源码

[java] view plaincopy
  1. /** 
  2.  * 我的适配器 
  3.  * @author haozi 
  4.  * 
  5.  */  
  6. public class MyAdapter extends BaseAdapter {  
  7.   
  8.     public static int PROGRESS;  
  9.     private Context context;  
  10.     private List<? extends Map<String, ?>> mData;  
  11.     private int mResource;  
  12.     private String[] mFrom;  
  13.     private int[] mTo;  
  14.     private LayoutInflater mLayoutInflater;  
  15.       
  16.     /** 
  17.      * 我的适配器的构造方法 
  18.      * @param context 调用方的上下文 
  19.      * @param data 数据 
  20.      * @param resource 
  21.      * @param from  
  22.      * @param to 
  23.      */  
  24.     public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){  
  25.           
  26.         this.context = context;  
  27.         this.mData = data;  
  28.         this.mResource = resource;  
  29.         this.mFrom = from;  
  30.         this.mTo = to;  
  31.         this.mLayoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);  
  32.     }  
  33.       
  34.     /** 
  35.      * 系统在绘制ListView之前,将会先调用getCount方法来获取Item的个数 
  36.      */  
  37.     public int getCount() {  
  38.           
  39.         return this.mData.size();  
  40.     }  
  41.   
  42.     public Object getItem(int position) {  
  43.           
  44.         return this.mData.get(position);  
  45.     }  
  46.   
  47.     public long getItemId(int position) {  
  48.           
  49.         return position;  
  50.     }  
  51.   
  52.     /** 
  53.      * 每绘制一个 Item就会调用一次getView方法, 
  54.      * 在此方法内就可以引用事先定义好的xml来确定显示的效果并返回一个View对象作为一个Item显示出来。 
  55.      * 也 正是在这个过程中完成了适配器的主要转换功能,把数据和资源以开发者想要的效果显示出来。 
  56.      * 也正是getView的重复调用,使得ListView的使用更 为简单和灵活。 
  57.      * 这两个方法是自定ListView显示效果中最为重要的,同时只要重写好了就两个方法,ListView就能完全按开发者的要求显示。 
  58.      * 而 getItem和getItemId方法将会在调用ListView的响应方法的时候被调用到。 
  59.      * 所以要保证ListView的各个方法有效的话,这两个方法也得重写。 
  60.      */  
  61.     public View getView(int position, View contentView, ViewGroup parent) {  
  62.           
  63.         contentView = this.mLayoutInflater.inflate(this.mResource, parent, false);    
  64.   
  65.         // 设置contentView的内容和样式,这里重点是设置contentView中文字的大小  
  66.         for(int index=0; index<this.mTo.length; index++){  
  67.             TextView textView = (TextView) contentView.findViewById(this.mTo[index]);  
  68.             textView.setText(this.mData.get(position).get(this.mFrom[index]).toString());  
  69.             if(index == 0){  
  70.                 textView.setTextSize(23+PROGRESS);  
  71.             }else if(index == 1){  
  72.                 textView.setTextSize(15+PROGRESS);  
  73.             }  
  74.         }  
  75.   
  76.         return contentView;  
  77.     }  
  78. }  

4 MainActivity源码

[java] view plaincopy
  1. /** 
  2.  * 入口Activity 
  3.  * @author haozi 
  4.  * 
  5.  */  
  6. public class MainActivity extends Activity {  
  7.       
  8.     private ListView mListView;  
  9.     private SeekBar mSeekBar;  
  10.     private MyAdapter myAdapter;  
  11.     private ArrayList<HashMap<String, String>> data;  
  12.       
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         // 代码和控件绑定  
  20.         this.mListView = (ListView) this.findViewById(R.id.listview01);  
  21.         this.mSeekBar = (SeekBar) this.findViewById(R.id.seekbar01);  
  22.   
  23.         // 准备数据  
  24.         initData();  
  25.         // 设置SeekBar变化监听  
  26.         this.mSeekBar.setOnSeekBarChangeListener(new MSeekBarOnSeekBarChangeListener());  
  27.     }  
  28.       
  29.     /** 
  30.      * 准备数据 
  31.      */  
  32.     private void initData(){  
  33.           
  34.         data = new ArrayList<HashMap<String,String>>();  
  35.         for(int index=0; index<5; index++){  
  36.             HashMap<String, String> map = new HashMap<String, String>();  
  37.             String textView01content = "textView01content" + index;  
  38.             String textView02content = "textView02content" + index;  
  39.             map.put("textView01", textView01content);  
  40.             map.put("textView02", textView02content);  
  41.             data.add(map);  
  42.         }  
  43.         // 创建适配器,并把数据交给适配器  
  44.         this.myAdapter = new MyAdapter(this, data, R.layout.main_listview01_item,   
  45.                 new String[]{"textView01""textView02"},   
  46.                 new int[]{R.id.textView01, R.id.textView02});  
  47.         // 为listView添加适配器  
  48.         this.mListView.setAdapter(this.myAdapter);  
  49.     }  
  50.       
  51.     /** 
  52.      * mSeekBar的变化监听 
  53.      * @author haozi 
  54.      * 
  55.      */  
  56.     class MSeekBarOnSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener{  
  57.   
  58.         public void onProgressChanged(SeekBar seekBar, int progress,  
  59.                 boolean fromUser) {  
  60.             // TODO Auto-generated method stub  
  61.             MyAdapter.PROGRESS = progress/10;  
  62.             MainActivity.this.mListView.setAdapter(MainActivity.this.myAdapter);  
  63.         }  
  64.   
  65.         public void onStartTrackingTouch(SeekBar seekBar) {  
  66.             // TODO Auto-generated method stub  
  67.               
  68.         }  
  69.   
  70.         public void onStopTrackingTouch(SeekBar seekBar) {  
  71.             // TODO Auto-generated method stub  
  72.               
  73.         }  
  74.     }  
  75. }  


demo下载链接地址:http://download.csdn.net/detail/hello_haozi/4290909

原创粉丝点击