简体   繁体   中英

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

You can use the dimens.xml resource file for this purpose. In your case you'll probably want to create a file called res/values-es/dimens.xml , and possibly also a -fr version. You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml , if you want to be more specific).

Example grabbed from the More Resource Types section on developer.android.com:

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

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

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. You need to click on the "Android" tab in the top, left corner of Android Studio and select " Project ". Then you need to go into " 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 ". A dialogue will come up, and for Directory name: type in values-es and click OK .

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

You would need to specify a different layout file in layout-es. That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. That layout file can then specify a theme, style, or text size on the views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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