Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

android - Dynamic Text size change according to tablet

With all effort,I finally reached to the end of my first app in android. And thanks to all. But after coming to end, I realized one thing that my app text size is common in all tablet sizes. So I will re frame my question.

Problem: I used my own custom text size in entire app. and some what satisfied with 7 inch tablet. But when I am looking the same thing in 8.9 inches and 10.1 inch tablet its containing lots of white spaces and text size are also relatively small. So is there some way that I can change the text size according to my tablet size??? It may look novice question but I am struggling with this because the same app which look wonderful in 7 inches, is loosing its essence in 8.9 and 10.1 inches. Or there is something else which I could do with my app for the same. Probable Solution:- As my topic indicates is there some way to change the text size as tablet size changes. Either dynamically or any other way.

EDITED:: As per Cheeta answer, approach is correct but I can't do it for each layout buttons and textfields and other field. There are hundreds of field like this in single app. Can I do it in some one place and call it required attribute. Is custom tag is approach which I am looking for.Is it possible????

Any help will be appreciated. Thanks in advance.

NOTE I have not reached to a answer but cheetah answer is somewhat leading to correct way. I am marking his answer as correct although there are few alignment issue with his answer. Any other answer is always welcome. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After checking some resources I finally came up with the following solution: Within the layout-xml I define the size of the button-text with a dimension declaration:

android:textSize="@dimen/campaign_textfontsize"

I created a dimens.xml in the "/values"-subdirectory with the corresponding value

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="campaign_textfontsize">18dp</dimen>
</resources>

then I created a second values-folder "/values-sw720dp" and copied the old dimens.xml into it. In this dimens.xml I adapted the fontsize value:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="campaign_textfontsize">24dp</dimen>
</resources>

Based on the current screensize android selects the proper dimens-value. So without any code, font is adapted to the display- (and display related button-) size.

Hope this helps...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...