前几天做项目,要实现一个设置字体属性的功能。其实这个做起来挺简单的,无非就是使用一个popWindow,在里面设置几个RadioGroup,RadioButton,实现选择,就行了。
是的,思路就是这样,不过在更改界面的字体属性时,我遇到了一个问题就是ListView中的TextView的字体属性,没有更改。
在网上查了许多的资料,最后终于找到了解决的方法。我们都知道BaseAdapter中有一个getView()方法。本来直接构造一个类,让它继承BaseAdapter,重写getView()方法就可以了。但是我的程序中,ListView包括ImageView和TextView,i且这个ImageView中的图像是动态生成的视频压缩图。我当时用了SimpleAdapter的setViewBinder()方法来处理图像的。
而 SimpleAdapter 是 BaseAdapter 的子类,所以我选择使用 SimpleAdapter 做为基类。具体方法如下:
public class MySimpleAdapter extends SimpleAdapter {
Context context ;
List<? extends Map<String, ?>> data;
int resource;
String[] from ;
int[] to;
LayoutInflater inflater = null ;//LayoutInflater 的作用类似于findViewById(),不过他是加载res/layout/***。xml文件,并实例化,
//而findViewById()则是加载当前布局中的某些控件
//我们可以使用LayoutInflater.inflater来加载一个未被加载及我们想要加载的布局。
private class ViewHolder
{
TextView textview;
ImageView imageview;
}
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
this.resource = resource;
this.from = from;
this.to = to;
inflater = LayoutInflater.from(context); //实例化这个控件
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String str_pattern = null;
int size = 18;
String str_style = null ;
String str_color = null;
ViewHolder viewholder;//ViewHolder可以使ListView的性能优化, 使用View 的findViewById()方法,加载太耗费性能,所以使用view的getTag()方法,比较方便。
SharedPreferences share = context.getSharedPreferences("jkwords", Activity.MODE_PRIVATE );//使用SharedPreferences来存储数据。
if( convertView == null )
{
viewholder = new ViewHolder( );
convertView = inflater.inflate(R.layout.video_list, null );//这里面的video_list就是我们要更改的ListView布局。
SharedPreferences.Editor edit = share.edit();//编辑文本
edit.clear().commit();//这边一定要clear之后,要提交,否则只是clear了其中的内容不提交的话,内容也不会有所修改的
convertView.setTag( viewholder );
}
else
{
viewholder = (ViewHolder)convertView.getTag( );
}
viewholder.textview = (TextView)convertView.findViewById( R.id.video_url );//ListView中的TextView控件,代表视频路径
viewholder.imageview =(ImageView)convertView.findViewById(R.id.video_imageview);//ListView中的ImageView控件,代表视频图像
viewholder.textview.setText( data.get(position).get("video_path").toString());
viewholder.imageview.setImageBitmap( (Bitmap) data.get(position).get("video_pic"));
str_pattern = share.getString("words_pattern", "custom");//得到存储的数据中的各个字段,str_pattern字体类型,size字体大小,str_style字体风格,str_color字体的颜色。
size = share.getInt("words_size", 18 );
str_style = share.getString("words_style", "标准");
str_color = share.getString("words_color", "黑色");
SettingWords setting = new SettingWords( str_pattern,size,str_style,str_color );//SettingWords是我封装好的一个设置字体属性的类,
setting.SettingStyle(viewholder.textview );//设置TextView字体的各种属性
return convertView;
}
}
其中SharePereferences类来存储我们选择的字体属性的到jkwords.xml中,在此类中只是直接使用存储的数据。
然后,在主程序中,我们直接使用这个适配器就可以了。
private SharedPreferences share ;
private ListView videolist ;
private MySimpleAdapter videoadapter;
在onCreate()方法中:
share = super.getSharedPreferences("jkwords",Activity.MODE_PRIVATE);//保存修改字体的信息到文本中
videoadapter = new MySimpleAdapter(
this,
list,
R.layout.video_list,
new String[]{ "video_pic","video_path"},
new int[]{ R.id.video_imageview,R.id.video_url});
videoadapter.setViewBinder( new ViewBinder(){
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// TODO Auto-generated method stub
if( (view instanceof ImageView)&&( data instanceof Bitmap ) )
{//instanceof 判断某一个实例是否为某种特定类型
ImageView imageview = ( ImageView )view;
imageview.setImageBitmap( (Bitmap)data);
return true;
}
return false;
}
});
videolist.setAdapter(videoadapter);
后面的弹出窗口代码略过,下面贴出当用户选择了字体的属性后,点击确定按钮的事件:
okay_btn.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton rb1 = (RadioButton)SearchResultActivity.popView.findViewById(wordpattern_radio.getCheckedRadioButtonId());
String str_pattern = rb1.getText().toString() ;
RadioButton rb2 = (RadioButton)SearchResultActivity.popView.findViewById(wordsize_radio.getCheckedRadioButtonId());
int size = Integer.parseInt( rb2.getText().toString() );
RadioButton rb3 = (RadioButton)SearchResultActivity.popView.findViewById(wordstyle_radio.getCheckedRadioButtonId());
String str_style = rb3.getText().toString( );
RadioButton rb4 = (RadioButton)SearchResultActivity.popView.findViewById(wordcolor_radio.getCheckedRadioButtonId());
String str_color = rb4.getText().toString();
SharedPreferences.Editor edit = share.edit( );//编辑文本
edit.putString("words_pattern", str_pattern);//保存字符串
edit.putInt("words_size", size);
edit.putString("words_style", str_style);
edit.putString("words_color", str_color);
edit.clear().commit();//删除旧数据后,提交更新
// SettingWords setting = new SettingWords( str_pattern,size,str_style,str_color );//设置字体的各种属性
videoadapter.notifyDataSetChanged( );//当执行nofityDataSetChanged方法时,就是调用了getView()方法。
popWin.dismiss( );//弹出窗口消失
}
});
这样就可以动态改变字体的属性了。
图1
图2
这样getView()调用的太频繁了,也将设置字体属性的代码放在外部,不要直接放在getView( )中。