您的位置:首页 > 移动开发 > Android开发

Android设置全局字体大小解决方案

2015-09-27 16:52 453 查看
今天是中秋,中秋还在研究代码的都比较苦逼,先Mark一下。
App中要用到字体大小设置,网上有很多文章。但百度,google了半天,对我来说,只有以下两篇有帮助,先附上链接: http://blog.sina.com.cn/s/blog_3e333c4a0101igti.html; http://blog.codecalculated.com/2013/07/09/dynamic-font-size-and-other-styles-in-an-android-app/
一篇中文,一篇英文。大家可以去看看。中文这篇讲的就比较通用,但是博主讲的不是很清楚,我琢磨了一阵才明白。于是决定将自己理解的分享出来,以做记录。
如果你的app中要用到多种字体大小,那么就需要先自己定义一些属性。新建attr.xml资源文件:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="fontsize_1" format="dimension"></attr>
<attr name="fontsize_2" format="dimension"></attr>
<attr name="fontsize_3" format="dimension"></attr>

</resources>


这里定义了三种字体大小,有需要可以再继续添加。
然后,我们需要在styles.xml里面继续定义:


<!-- 设置字体大小 theme -->
<style name="Theme_Small">
<item name="fontsize_1">15sp</item>
<item name="fontsize_2">10sp</item>
<item name="fontsize_3">8sp</item>
</style>
<style name="Theme_Medium">
<item name="fontsize_1">25sp</item>
<item name="fontsize_2">20sp</item>
<item name="fontsize_3">15sp</item>
</style>
<style name="Theme_Large">
<item name="fontsize_1">45sp</item>
<item name="fontsize_2">30sp</item>
<item name="fontsize_3">25sp</item>
</style>

<!-- 为相应的控件设置style -->
<style name="title_text">
<item name="android:textSize">?@attr/fontsize_1</item>
</style>
<style name="subtitle_text">
<item name="android:textSize">?@attr/fontsize_2</item>
</style>
<style name="article_text">
<item name="android:textSize">?@attr/fontsize_3</item>
</style>


如上所示,我建立了三个控制字体大小的theme:Theme_Small、Theme_Medium、Theme_Large,每个theme里都对attr里自定义的属性值进行了赋值。然后又对需要变更文字大小的控件设立相应的style:title_text、subtitle_text、article_text,在layout xml文件中将控件设置对应的style即可。如我的布局是这样的:


<?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:orientation="vertical" >
<TextView
style="@style/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是一行测试用字"/>
<TextView
style="@style/subtitle_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是一行测试用字"/>
<TextView
style="@style/article_text"
android:layout_width="match_parent"
android:lay
4000
out_height="wrap_content"
android:text="这是一行测试用字"/>

</LinearLayout>


最后,在activity里setContentView之前设置主题即可:


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int mode=-1;
try{
mode=getIntent().getIntExtra("textsize", 1);
}catch(NullPointerException e){
e.printStackTrace();
}catch (Exception e) {

}
if(mode==-1||mode==1){
this.setTheme(R.style.Theme_Small);
}else if(mode==2){
this.setTheme(R.style.Theme_Medium);
}else {
this.setTheme(R.style.Theme_Large);
}
setContentView(R.layout.activity_showtext);
}


大家在使用的时候可以设置一个基类BaseActivity,并通过sharedpreferenes保存相应的设置值,来对全局字体进行设置。


附上Demo供大家参考,下载链接:

http://download.csdn.net/download/mrwangxsyz/9142853
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  app-全局-字体