是否可以在显示文本的所有视图中使用字体大小的应用程序范围设置? 我想提供一个偏好给用户,它应该允许缩放应用程序中的所有文本。Android:应用程序范围的字体大小首选项
Android明确允许将"sp" dimension unit用于可缩放文本,但是没有实际的方式来以全局方式设置“用户的字体大小首选项”。
通过对活动的实例化所有视图迭代是不是一个真正的选择;-)
是否可以在显示文本的所有视图中使用字体大小的应用程序范围设置? 我想提供一个偏好给用户,它应该允许缩放应用程序中的所有文本。Android:应用程序范围的字体大小首选项
Android明确允许将"sp" dimension unit用于可缩放文本,但是没有实际的方式来以全局方式设置“用户的字体大小首选项”。
通过对活动的实例化所有视图迭代是不是一个真正的选择;-)
这里这是我为我的应用程序做的。 用几句话 - 在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" />
我希望我的回答能帮到你。
是的,这是可能的。要做到这一点,你需要:
TextView
像:
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);
这适用于出现在TextView中的文本,但是像EditText,Button,CheckedTextView等所有的TextView子类呢?你基本上需要创建你使用的每个Widget类型的子类。 – 2011-02-02 18:10:20
@Mayra:那个正确的 - 我被迫这样做......我的意思是它不是很有趣,但结果令人着迷:) – barmaley 2011-02-02 19:46:30
从这里臀部拍摄的想法考虑(没有自定义的TextView实施所需)
onCreate
方法获取财产的价值,并保存为一个场沿着同样的路线说,你想让它动态工作吗?创建一个简单的类(说TextSetter)持有内部列表,并有3种方法:添加,删除和的setSize
Activity#onCreate
确定要调整和使用TextSetter#set
将其添加到列表中
可能的重复[从代码动态更改多个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
这个问题已经被讨论了很多次,参见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