Font size of text

2019-03-06 10:22发布

I have created different layouts (layout, layout-small, layout-normal, layout-large, layout-xlarge) and for values (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi). I have one activity in my app to show text. I have scroll on text. And I set the values of text size in values-mdpi. But when I run my app on emulator 3.2"QVGA(ADP2)(320 x 480:mdpi) scroll on text work. But when I run my app on emulator 5.1"WVGA(480 x 800:mdpi) all text on half screen and the size of text is small. I think android picking layout depending upon size and text size depending upon values-mdpi. I want size of text big on large screen although they belong mdpi.

My whole app is in portrait mode. And also same case with ldpi and hdpi. Please provide general solution.

8条回答
家丑人穷心不美
2楼-- · 2019-03-06 10:27

Try like this

STEP : 1 first in res folder => values => dimensions.xml=> create dimensions.xml file to values folder in that create like below code depending on textsize

     <?xml version="1.0" encoding="utf-8"?>
       <resources>
           <dimen name="textsize">8sp</dimen>
           <dimen name="text">10sp</dimen>
           <dimen name="comments">7sp</dimen>
       </resources>

STEP : 2 in java file write this line

      TextView textviewtwo = (TextView)findViewById(R.id.sponsertwo_txt);
textviewtwo.setText("brought to you by");    
textviewtwo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
  getResources().getDimension(R.dimen.textsize));
      // in place of textsize use text and comments what ever we want depending on size. use like text size                  adjust automatically in all devices 
查看更多
Melony?
3楼-- · 2019-03-06 10:27

Actually you don't need to create all these layout directories to handle text size. Just stick with the normal one (layout) and create different values directories with different text size for each resolution you want to support. so you have to add text size in other values directories.

At run-time Android will pick the correct text size.

查看更多
狗以群分
4楼-- · 2019-03-06 10:32

you are using only portrait version, if you are using same structure of design for all devices you don't need to add four different layout for each pixel density.

create android values folders that supports different screen sizes.

enter image description here

Here first folder for 7inch android devices that have ANDROID version 3.1 or less. second one is for 7inch devices that have android version greater than 3.1. naming convention sw600 means smallest width of 600. so this folder works for width size 600 to 720. So create values-sw480dp and values-sw320 add values to dimens.xml file.

Android runtime automatically picks values from appropriate folder.

查看更多
来,给爷笑一个
5楼-- · 2019-03-06 10:35

Just instead yourvalues (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi), which are specifying the text font sizes for each density.

Use text size specification based on screen size, like this:values (values, values-small, values-normal, values-large, values-xlarge)

Alternatively to obtain better results for various devices you can combine density with screen size, like this:values-normal-mdpi

That's all

查看更多
Rolldiameter
6楼-- · 2019-03-06 10:35

Use this code, it works based on screen size:

Display display = getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();

yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,(float)(displayWidth * 6/100));
查看更多
放荡不羁爱自由
7楼-- · 2019-03-06 10:40

Create folder like values-mdpi , values-w360dp-mdpi in res folder.

create dimens.xml on both folder.

and paste it below code in values-mdpi dimens.xml

<?xml version="1.0" encoding="utf-8"?>
       <resources>
           <dimen name="textsize">14sp</dimen>

       </resources>

and paste it below code in values-w360dp-mdpi dimens.xml

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

then apply text size on your activity

android:textSize="@dimen/textsize"
查看更多
登录 后发表回答