我已经创建了从"8“到"46”字体大小列表的微调。我可以点击字体大小,然后在微调器中显示出来。
我的需求是,如果我点击一个微调器内的字体大小"26“,那么它应该应用于我的整个项目。例如应用到我的屏幕,文本视图外观,编辑文本-粗体/斜体等。再次如果我点击46大小,那么它应该应用到我的整个项目。
我怎么才能通过编程来做到这一点呢?
发布于 2012-10-03 18:12:54
可能的解决方案是创建一个基类,它是对TextView的扩展,并使用这个文本视图类作为编辑文本。希望你在第一个屏幕上询问大小。在任何情况下,您都可以在基类中设置文本大小。这将解决您的问题。
就像您在com.example包中创建这个类,类名是BaseTextView,那么在xml文件中而不是<TextView .../>中,您将编写<com.example.BaseTextView ... />
希望这能有所帮助。
发布于 2014-07-04 17:51:24
Android文档没有详细说明通过用户在应用程序级别的选择来全局更改字体大小的最有效方法。
我认为黑魔给出的answer有一个问题。
问题是许多Android小部件的子类是TextView,比如Button、RadioButton和CheckBox。其中一些是TextView的间接子类,这使得在这些类中实现自定义版本的TextView非常困难。
然而,正如的Siddharth Lele在他的评论中指出的那样,使用styles或themes是处理整个应用程序中文本大小变化的更好方法。
我们设置布局的样式来控制视图的外观。主题本质上就是这些样式的集合。但是,我们可以将主题仅用于文本大小设置;而无需为每个属性定义值。使用主题而不是样式为我们提供了一个巨大的优势:我们可以通过编程的方式为整个视图设置一个主题。
theme.xml
<resources>
<style name="FontSizeSmall">
<item name="android:textSize">12sp</item>
</style>
<style name="FontSizeMedium">
<item name="android:textSize">16sp</item>
</style>
<style name="FontSizeLarge">
<item name="android:textSize">20sp</item>
</style>
</resources>创建一个类来处理加载我们的首选项:
public class BaseActivity extends Activity {
@Override
public void onStart() {
super.onStart();
// Enclose everything in a try block so we can just
// use the default view if anything goes wrong.
try {
// Get the font size value from SharedPreferences.
SharedPreferences settings =
getSharedPreferences("com.example.YourAppPackage", Context.MODE_PRIVATE);
// Get the font size option. We use "FONT_SIZE" as the key.
// Make sure to use this key when you set the value in SharedPreferences.
// We specify "Medium" as the default value, if it does not exist.
String fontSizePref = settings.getString("FONT_SIZE", "Medium");
// Select the proper theme ID.
// These will correspond to your theme names as defined in themes.xml.
int themeID = R.style.FontSizeMedium;
if (fontSizePref == "Small") {
themeID = R.style.FontSizeSmall;
}
else if (fontSizePref == "Large") {
themeID = R.style.FontSizeLarge;
}
// Set the theme for the activity.
setTheme(themeID);
}
catch (Exception ex) {
ex.printStackTrace();
}
}最后,通过扩展BaseActivity来创建活动,如下所示:
public class AppActivity extends BaseActivity{
}因为大多数应用程序的活动数量比继承TextView的TextViews或小部件要少得多。随着复杂性的增加,这将是指数级的,因此此解决方案需要较少的代码更改。
感谢Ray Kuhnell
发布于 2018-04-08 02:09:26
你可以使用基本活动配置来放大/缩小你的应用程序的文本大小,使所有的活动成为固有的基本活动。
Scale normal值为1.0,2.0将使字体大小加倍,而.50将使其减半。
public void adjustFontScale( Configuration configuration,float scale) {
configuration.fontScale = scale;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adjustFontScale( getResources().getConfiguration());
}https://stackoverflow.com/questions/12704216
复制相似问题