Font size of text

2019-03-06 10:20发布

问题:

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.

回答1:

Just create layout folders like layout, layout-small, layout-large, layout-xlarge then create values folders like values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi. Then you can create values folder depending upon height or width and belongs to ldpi or mdpi i.e values-w360dp-mdpi or values-h600dp-mdpi. Then android automatically picks layout depending upon screen size and values on depending height or width.



回答2:

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.

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.



回答3:

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"


回答4:

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



回答5:

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 


回答6:

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.



回答7:

Try to add this in your AndroidManifest.xml

<supports-screens android:anyDensity="true"
                  android:largeScreens="true"
                  android:normalScreens="true"
                  android:resizeable="true"
                  android:smallScreens="true"/>


回答8:

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));