2011-02-02 39 views
21

是否可以在显示文本的所有视图中使用字体大小的应用程序范围设置? 我想提供一个偏好给用户,它应该允许缩放应用程序中的所有文本。Android:应用程序范围的字体大小首选项

Android明确允许将"sp" dimension unit用于可缩放文本,但是没有实际的方式来以全局方式设置“用户的字体大小首选项”。

通过对活动的实例化所有视图迭代是不是一个真正的选择;-)

+0

可能的重复[从代码动态更改多个textview的大小(没有“在磁盘上”xml主题)?](http://stackoverflow.com/questions/4473397/dynamically-change-size-of-multiple- textview-from-code-without-a-on-disk-xml -t) – 2011-02-02 17:15:17

+0

这个问题已经被讨论了很多次,参见http://stackoverflow.com/questions/4473397/dynamically-change-size-of- multiple-textview-from-code-without-a-on-disk-xml-t查看其他相关链接的列表。 – 2011-02-02 17:15:34

回答

46

这里这是我为我的应用程序做的。 用几句话 - 在Activity.onCreate()中,您可以获取具有特定字体大小的样式的资源ID,并将此样式应用于活动主题。然后通过首选项活动,您可以在这些组之间切换。

首先在值/ attrs。

<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> 

然后在值/ styles.xml声明几集的字体大小:对于集的字体大小的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> 
在每一个活动的 onCreate()方法

然后加入:

getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true); 

其中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(); 
    } 
} 

和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; 
    } 
} 

而且FontStyle.values()用作您的PreferencesActivity物品Spinner。 那是我的样子:

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()); 
} 
} 

最后,你可以用你的字体大小的喜好:

<TextView android:textSize="?attr/font_large" /> 

或者我更喜欢使用样式,价值观/ 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> 

你可以这样使用它:

<TextView style="@style/Label.XLarge" /> 

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

6

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

  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; 
    } 
} 

而现在徘徊无论你可以在所有全文视图中将全部文本大小全局更改为20,只需拨打:

SimpleTextView.setGlobalTextSize(20); 
+4

这适用于出现在TextView中的文本,但是像EditText,Button,CheckedTextView等所有的TextView子类呢?你基本上需要创建你使用的每个Widget类型的子类。 – 2011-02-02 18:10:20

+0

@Mayra:那个正确的 - 我被迫这样做......我的意思是它不是很有趣,但结果令人着迷:) – barmaley 2011-02-02 19:46:30

1

从这里臀部拍摄的想法考虑(没有自定义的TextView实施所需)

  1. 财产申报像UNIVERSAL_FONT_SIZE的想法,它可以从设置中更改,但应用程序调用之间将保留
  2. 在每个活动的onCreate方法获取财产的价值,并保存为一个场
  3. 让你的代码中使用,对于每个文本调整大小的组件
  4. 没有将采取行动无法阻止您创建多个属性,如BUTTONS_TXT_SIZE,TEXT_SIZE,LIST_TXT_SIZE等,然后具有逻辑,例如文本增加的百分比并为每种控件计算适当的大小(因为对于不同的控件可能有不同的大小)

沿着同样的路线说,你想让它动态工作吗?创建一个简单的类(说TextSetter)持有内部列表,并有3种方法:添加,删除和的setSize

  1. Activity#onCreate确定要调整和使用TextSetter#set将其添加到列表中
  2. 当每个控制用户想要从菜单中增加/减小字体大小,当你处理刚才执行的TextSetter#setSize时,你将通过控制列表循环,检测它是哪种类型并相应地调整文本大小
0

对于那些使用@mixels答案的自定义属性来扩大视图的问题。

如果您的视图位于片段中,则还需要在片段的onCreateView()中应用FontStyle,或者在应用程序的主题中设置这些属性的值。

更多详情请参阅我的aswer here

相关问题