简体   繁体   English

使用不同的语言区域设置更改textSize

[英]change textSize with different language locale

I have added spanish and french to my app but some of the wording is longer in spanish then english. 我已经在我的应用程序中添加了西班牙语和法语,但是一些措辞比西班牙语更长,然后是英语。 how can i change the textsize when the values-es/string.xml file is accessed 如何在访问values-es / string.xml文件时更改textsize

You can use the dimens.xml resource file for this purpose. 您可以使用dimens.xml资源文件来实现此目的。 In your case you'll probably want to create a file called res/values-es/dimens.xml , and possibly also a -fr version. 在您的情况下,您可能想要创建一个名为res/values-es/dimens.xml ,也可能还要创建一个-fr版本的文件。 You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml , if you want to be more specific). 您可以在res/values/dimens.xml (或res/values-en/dimens.xml dimens.xml中)指定默认值,如果您想更具体的话。

Example grabbed from the More Resource Types section on developer.android.com: 从developer.android.com上的“ 更多资源类型”部分获取的示例:

dimens.xml dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_height">25dp</dimen>
    <dimen name="textview_width">150dp</dimen>
    <dimen name="ball_radius">30dp</dimen>
    <dimen name="font_size">16sp</dimen>
</resources> 

Apply in xml 在xml中应用

<TextView
    android:layout_height="@dimen/textview_height"
    android:layout_width="@dimen/textview_width"
    android:textSize="@dimen/font_size"/>

Or in code 或者在代码中

float fontSize = getResources().getDimension(R.dimen.font_size);

There are also solutions here on SO that use a iterative/recursive process to shrink the text size of a TextView to 'fit' in its bounding box (using a custom view), but I'd say above is a more robust approach, especially if you're considering adding more languages in the future. SO上也有解决方案,使用迭代/递归过程将TextView的文本大小缩小到其边界框中“适合”(使用自定义视图),但我上面说的是一种更强大的方法,尤其是如果您考虑将来添加更多语言。

The above explanations are correct, but they don't fully explain how to do it. 以上解释是正确的,但它们并没有完全解释如何做到这一点。

When you open your project in Android Studio, then it automatically shows this project in the " Android " mode. 当您在Android Studio中打开项目时,它会自动以“ Android ”模式显示该项目。 You need to click on the "Android" tab in the top, left corner of Android Studio and select " Project ". 您需要点击Android Studio左上角的“Android”标签,然后选择“ 项目 ”。 Then you need to go into " app>src>main>res ". 然后你需要进入“ app> src> main> res ”。 Then you need to right-click on the " res " folder and from the menu that comes up, select " New>Android resource directory ". 然后,您需要右键单击res ”文件夹,然后从出现的菜单中选择“ 新建> Android资源目录 ”。 A dialogue will come up, and for Directory name: type in values-es and click OK . 将出现一个对话框,对于目录名称:键入values-es并单击“ 确定”

This will create a folder for all Spanish Locale values. 这将为所有西班牙语语言环境值创建一个文件夹。 And then you can right-click on this values-es folder to create dimens.xml , string.xml , color.xml , ...etc. 然后,您可以右键单击values-es文件夹以创建dimens.xmlstring.xmlcolor.xml等。 files that will be used whenever Spanish Locale is selected in the phone. 在手机中选择西班牙语区域设置时将使用的文件。

If you've already created a string.xml file for Spanish Locale through the graphical user interface, then the values-es folder with string.xml file will already be in the Project, when you go there. 如果您已经通过图形用户界面为西班牙语语言环境创建了一个string.xml文件,则当您去那里时,带有string.xml文件的values-es文件夹已经在项目中。 And in this case, you just need to right-click on the values-es folder to create the dimens.xml file there for Spanish Locale. 在这种情况下,您只需右键单击 values-es文件夹即可为其创建西班牙语区域设置的dimens.xml文件。

You would need to specify a different layout file in layout-es. 您需要在layout-es中指定不同的布局文件。 That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. 这样,当Android从values-es / string.xml中提取时,它将加载不同的layout-es / yourfile.xml。 That layout file can then specify a theme, style, or text size on the views. 然后,该布局文件可以在视图上指定主题,样式或文本大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM