很多app有这种需求,实现字体小中大字体设置,仿照QQ的字体。经过测试,下面代码完美实现,需要重启APP才能生效。
上核心代码
public class MainActivity extends Activity {
@InjectView(R.id.btn_skip1)
Button btnSkip1;
@InjectView(R.id.btn_skip2)
Button btnSkip2;
@InjectView(R.id.btn_skip3)
Button btnSkip3;
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int state = SpUtils.getSPValue(this,"s",1);
if(state==1){
setTheme(R.style.Default_TextSize_Small);
}else if(state==2){
setTheme(R.style.Default_TextSize_Middle);
}else{
setTheme(R.style.Default_TextSize_Big);
}
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@OnClick({R.id.btn_skip1, R.id.btn_skip2, R.id.btn_skip3})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_skip1:
SpUtils.putSPValue(this,"s",1);
break;
case R.id.btn_skip2:
SpUtils.putSPValue(this,"s",2);
break;
case R.id.btn_skip3:
SpUtils.putSPValue(this,"s",3);
break;
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="我是主页面"
android:textSize="?font10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="我是主页面"
android:textSize="?font11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="我是主页面"
android:textSize="?font12" />
<Button
android:id="@+id/btn_skip1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="smallSize" />
<Button
android:id="@+id/btn_skip2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="normalSize" />
<Button
android:id="@+id/btn_skip3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="largeSize" />
</LinearLayout>
新建attrs文件,定义字体属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="font10" format="dimension" />
<attr name="font11" format="dimension" />
<attr name="font12" format="dimension" />
<attr name="font13" format="dimension" />
<attr name="font14" format="dimension" />
<attr name="font15" format="dimension" />
<attr name="font16" format="dimension" />
<attr name="font17" format="dimension" />
<attr name="font18" format="dimension" />
<attr name="font19" format="dimension" />
</resources>
自定义style属性
<style name="Default_TextSize_Small">
<item name="font10">@dimen/font_size_9sp</item>
<item name="font11">@dimen/font_size_9sp</item>
<item name="font12">@dimen/font_size_9sp</item>
<item name="font13">@dimen/font_size_12sp</item>
<item name="font14">@dimen/font_size_13sp</item>
<item name="font15">@dimen/font_size_14sp</item>
<item name="font16">@dimen/font_size_15sp</item>
<item name="font17">@dimen/font_size_16sp</item>
<item name="font18">@dimen/font_size_17sp</item>
<item name="font19">@dimen/font_size_18sp</item>
</style>
<style name="Default_TextSize_Middle">
<item name="font10">@dimen/font_size_15sp</item>
<item name="font11">@dimen/font_size_16sp</item>
<item name="font12">@dimen/font_size_17sp</item>
<item name="font13">@dimen/font_size_13sp</item>
<item name="font14">@dimen/font_size_14sp</item>
<item name="font15">@dimen/font_size_15sp</item>
<item name="font16">@dimen/font_size_16sp</item>
<item name="font17">@dimen/font_size_17sp</item>
<item name="font18">@dimen/font_size_18sp</item>
<item name="font19">@dimen/font_size_19sp</item>
</style>
<style name="Default_TextSize_Big">
<item name="font10">@dimen/font_size_18sp</item>
<item name="font11">@dimen/font_size_18sp</item>
<item name="font12">@dimen/font_size_18sp</item>
<item name="font13">@dimen/font_size_14sp</item>
<item name="font14">@dimen/font_size_15sp</item>
<item name="font15">@dimen/font_size_16sp</item>
<item name="font16">@dimen/font_size_17sp</item>
<item name="font17">@dimen/font_size_18sp</item>
<item name="font18">@dimen/font_size_19sp</item>
<item name="font19">@dimen/font_size_20sp</item>
</style>
定义dimen文件
<dimen name="font_size_21sp">21sp</dimen>
<dimen name="font_size_20sp">20sp</dimen>
<dimen name="font_size_19sp">19sp</dimen>
<dimen name="font_size_18sp">18sp</dimen>
<dimen name="font_size_17sp">17sp</dimen>
<dimen name="font_size_16sp">16sp</dimen>
<dimen name="font_size_15sp">15sp</dimen>
<dimen name="font_size_14sp">14sp</dimen>
<dimen name="font_size_13sp">13sp</dimen>
<dimen name="font_size_12sp">12sp</dimen>
<dimen name="font_size_11sp">11sp</dimen>
<dimen name="font_size_10sp">10sp</dimen>
<dimen name="font_size_9sp">9sp</dimen>
就是这样子,谢谢阅览。有其他问题欢迎留言,也可以小小打赏一下哦,你的鼓励是我最大的进步。
邮箱:845561290@qq.com