工作上需要动态改变app字体大小,而我的LAC也可能需要这个,于是半公半私地看起来...google了很多页面,越发觉得Theme和Style神奇了...
咱也讲不好,还是直接放上几段代码做记录吧...
styles.xml
<resources>
<style name="small_title_text">
<item name="android:textSize">22sp</item>
<item name="android:textColor">@color/color1</item>
<item name="android:textStyle">normal</item>
<item name="android:paddingBottom">5dip</item>
</style>
<style name="small_body_text">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/color1</item>
<item name="android:textStyle">normal</item>
<item name="android:paddingBottom">5dip</item>
</style>
<style name="large_title_text">
<item name="android:textSize">40sp</item>
<item name="android:textColor">@color/color</item>
<item name="android:textStyle">normal</item>
<item name="android:paddingBottom">5dip</item>
</style>
<style name="large_body_text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/color</item>
<item name="android:textStyle">normal</item>
<item name="android:paddingBottom">5dip</item>
</style>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:Theme">
</style>
<style name="Theme.Small" parent="Theme">
<item name="textTitle">@style/small_title_text</item>
<item name="textBody">@style/small_body_text</item>
</style>
<style name="Theme.Large" parent="Theme">
<item name="textTitle">@style/large_title_text</item>
<item name="textBody">@style/large_body_text</item>
</style>
</resources>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textTitle" format="reference" />
<attr name="textBody" format="reference" />
</resources>
main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
style="?textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
style="?textBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:text="ToggleButton" />
</RelativeLayout>
main.java
protected void onButtonClick() {
if (toggle) {
this.setTheme(R.style.Theme_Large);
setContentView(R.layout.activity_main);
} else {
this.setTheme(R.style.Theme_Small);
setContentView(R.layout.activity_main);
}
Button btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
onButtonClick();
}
});
toggle = !toggle;
}
file://C:\Program Files\sdk\platforms\android-17\data\res
http://developer.android.com/guide/topics/ui/themes.html
http://stackoverflow.com/questions/3241729/android-dynamically-change-style-at-runtime