Skip to content

Commit

Permalink
add setting for font size: large / normal / small
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed Jun 6, 2016
1 parent cd5a5cd commit 687f65b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class MyPreferenceFragment extends PreferenceFragmentCompat {
CheckBoxPreference launch_hottopic_as_entry;
CheckBoxPreference daynight_control;
CheckBoxPreference setting_post_navigation_control;
ListPreference setting_fontsize_control;
CheckBoxPreference image_quality_control;

CheckBoxPreference notification_control_mail;
Expand Down Expand Up @@ -118,6 +120,28 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});


setting_fontsize_control = (ListPreference) findPreference("setting_fontsize_control");
setting_fontsize_control.setValueIndex(Settings.getInstance().getFontIndex());
setting_fontsize_control.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int fontIndex = Settings.getInstance().getFontIndex();
if(newValue instanceof String){
fontIndex = Integer.parseInt((String)newValue);
}
Settings.getInstance().setFontIndex(fontIndex);

// recreate activity for font size to take effect
Activity activity = getActivity();
if(activity != null) {
activity.recreate();
}
return true;
}
});


image_quality_control = (CheckBoxPreference) findPreference("setting_image_quality_control");
image_quality_control.setChecked(Settings.getInstance().isLoadOriginalImage());
image_quality_control.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/zfdang/zsmth_android/SMTHBaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.zfdang.zsmth_android;

import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/**
* Created by zfdang on 2016-5-10.
Expand Down Expand Up @@ -34,4 +37,15 @@ protected void onDestroy() {

dismissProgress();
}

@Override
public Resources getResources() {
Resources res = super.getResources();
Configuration config = res.getConfiguration();
config.fontScale = Settings.getInstance().getFontSizeFloatValue();
res.updateConfiguration(config, res.getDisplayMetrics());

Log.d("SMTHBaseActivity", "getResources: " + config.fontScale);
return res;
}
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/zfdang/zsmth_android/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@ public void setPostNavBar(boolean bPostNavBar) {
}
}

private static final String ZSMTH_FONT_INDEX = "ZSMTH_FONT_INDEX";
private int iFontIndex; // 0: large font; 1: normal font; 2: small
public int getFontIndex() {
return iFontIndex;
}
public float getFontSizeFloatValue() {
if(iFontIndex == 0) {
return 1.15f;
} else if(iFontIndex == 2) {
return 0.85f;
} else {
return 1.0f;
}
}
public void setFontIndex(int iFontIndex) {
System.out.println(iFontIndex);
if(this.iFontIndex != iFontIndex) {
this.iFontIndex = iFontIndex;
mEditor.putInt(ZSMTH_FONT_INDEX, this.iFontIndex);
mEditor.commit();
}
}


private final String Preference_Name = "ZSMTH_Config";

Expand Down Expand Up @@ -332,5 +355,6 @@ private void initSettings(){
bLaunchHotTopic = mPreference.getBoolean(LAUNCH_HOTTOPIC_AS_ENTRY, true);

bPostNavBar = mPreference.getBoolean(SHOW_POST_NAVITATION_BAR, true);
iFontIndex = mPreference.getInt(ZSMTH_FONT_INDEX, 1);
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="font_size_names">
<item>大字体</item>
<item>正常字体</item>
<item>小字体</item>
</string-array>
<string-array name="font_size_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
android:summaryOn="快速导航栏已打开"
android:title="帖子快速导航栏"/>

<ListPreference android:title="字体设置"
android:key="setting_fontsize_control"
android:summary="%s"
android:entries="@array/font_size_names"
android:entryValues="@array/font_size_values">
</ListPreference>

<CheckBoxPreference
android:defaultValue="on"
android:key="setting_image_quality_control"
Expand Down

0 comments on commit 687f65b

Please sign in to comment.