Android:应用程序范围的字体大小首选项

ge0*_*0rg 23 android preferences font-size

是否可以对显示文本的所有视图使用的font-size进行应用程序范围的设置?我想为用户提供一个Preference,它应该允许缩放应用程序中的所有文本.

Android明确允许使用"sp"维度单元来扩展文本,但是没有实际的方法来以全局方式设置"用户的字体大小首选项".

迭代关于Activity实例化的所有视图实际上不是一个选项;-)

mix*_*xel 50

这就是我为我的应用程序制作它的方式.换句话说 - 在Activity.onCreate()您获得具有特定字体大小的样式的资源ID,并将此样式应用于活动主题.然后使用首选项活动,您可以在这些集之间切换.

首先,在values/attrs.xml中声明字体大小集的属性:

<declare-styleable name="FontStyle">
    <attr name="font_small" format="dimension" />
    <attr name="font_medium" format="dimension" />
    <attr name="font_large" format="dimension" />
    <attr name="font_xlarge" format="dimension" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

然后在values/styles.xml中声明几组字体大小:

<style name="FontStyle">
</style>

<style name="FontStyle.Small">
    <item name="font_small">14sp</item>
    <item name="font_medium">16sp</item>
    <item name="font_large">18sp</item>
    <item name="font_xlarge">20sp</item>
</style>

<style name="FontStyle.Medium">
    <item name="font_small">18sp</item>
    <item name="font_medium">20sp</item>
    <item name="font_large">22sp</item>
    <item name="font_xlarge">24sp</item>
</style>

<style name="FontStyle.Large">
    <item name="font_small">26sp</item>
    <item name="font_medium">28sp</item>
    <item name="font_large">30sp</item>
    <item name="font_xlarge">32sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在onCreate()每个活动的方法中添加:

getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true);
Run Code Online (Sandbox Code Playgroud)

对象Preferences的外观在哪里SharedPreferences:

public class Preferences {
    private final static String FONT_STYLE = "FONT_STYLE";

    private final Context context;

    public Preferences(Context context) {
        this.context = context;
    }

    protected SharedPreferences open() {
        return context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    }

    protected Editor edit() {
        return open().edit();
    }

    public FontStyle getFontStyle() {
        return FontStyle.valueOf(open().getString(FONT_STYLE,
            FontStyle.Medium.name()));
    }

    public void setFontStyle(FontStyle style) {
        edit().putString(FONT_STYLE, style.name()).commit();
    }
}
Run Code Online (Sandbox Code Playgroud)

和FontStyle是:

public enum FontStyle {
    Small(R.style.FontStyle_Small, "Small"), 
    Medium(R.style.FontStyle_Medium, "Medium"), 
    Large(R.style.FontStyle_Large, "Large");

    private int resId;
    private String title;

    public int getResId() {
        return resId;
    }

    public String getTitle() {
        return title;
    }

    FontStyle(int resId, String title) {
        this.resId = resId;
        this.title = title;
    }
}
Run Code Online (Sandbox Code Playgroud)

FontStyle.values()用作Spinner你的物品PreferencesActivity.这就是我的样子:

public class PreferencesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.preferences);

    Preferences prefs = new Preferences(this);

    Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles);
    FontStylesAdapter adapter = new FontStylesAdapter(this,
            R.layout.font_styles_row, FontStyle.values());
    fontStylesView.setAdapter(adapter);

    fontStylesView.setSelection(prefs.getFontStyle().ordinal());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.preferences, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_done:
        onMenuDone();
        finish();
        return true;
    case R.id.menu_cancel:
        finish();
        return true;
    default:
        return false;
    }
}

private void onMenuDone() {
    Preferences prefs = new Preferences(this);

    Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles);
    prefs.setFontStyle((FontStyle) fontStylesView.getSelectedItem());
}
}
Run Code Online (Sandbox Code Playgroud)

最后,您可以使用您的字体大小首选项:

<TextView android:textSize="?attr/font_large" />
Run Code Online (Sandbox Code Playgroud)

或者我更喜欢使用样式,在values/styles.xml中添加:

<style name="Label" parent="@android:style/Widget.TextView">
    <item name="android:textSize">?attr/font_medium</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

<style name="Label.XLarge">
    <item name="android:textSize">?attr/font_xlarge</item>
</style>
Run Code Online (Sandbox Code Playgroud)

你可以这样使用它:

<TextView style="@style/Label.XLarge" />
Run Code Online (Sandbox Code Playgroud)

我希望我的回答能帮到你.


Bar*_*ica 5

是的,这是可能的.要做到这一点,你需要:

  1. 声明你自己的类扩展 TextView
  2. 仅在所有对话框/活动中使用它

喜欢:

public class SimpleTextView extends TextView
{
    private static final float DEFAULT_TEXT_SIZE=12.0;
    private static float textSize=DEFAULT_TEXT_SIZE;

    public SimpleTextView(Context context)
    {
        super(context);
        this.setTextSize(textSize);
    }

    public SimpleTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.setTextSize(textSize);
    }

    public SimpleTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        this.setTextSize(textSize);
    }

    public static void setGlobalSize(float size)
    {
        textSize=size;
    }

    public static float getGlobalSize()
    {
        return textSize;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,无论您是谁,您都可以在所有文本视图中全局将所有文本大小更改为20,只需调用:

SimpleTextView.setGlobalTextSize(20);
Run Code Online (Sandbox Code Playgroud)

  • 这适用于出现在`TextView中的文本,但是所有的`TextView`子类如`EditText`,`Button`,`CheckedTextView`等等呢?您基本上需要创建您使用的每个Widget类型的子类. (4认同)