简体   繁体   English

适用于不同屏幕的Android文本大小

[英]Android Text Sizes for different screens

Hey I know there's a lot of questions pertaining to this topic, I just need some clarification. 嘿,我知道与此主题相关的问题很多,我只需要澄清一下即可。

I'm trying to get my app to look as closely the same on all device sizes. 我正在尝试让我的应用在所有设备尺寸上看起来都一样。 I use 'sp' as the type of value for "textSize", but it doesn't seem to do anything. 我将“ sp”用作“ textSize”的值类型,但它似乎无能为力。 It looks too big on ldpi, and hdpi screen sizes, and too small on mdpi, and xxhdpi. 在ldpi和hdpi屏幕尺寸上看起来太大,在mdpi和xxhdpi上看起来太小。 It looks like how I'd like it to look on hdpi, and xhdpi screens. 看起来像我希望它在hdpi和xhdpi屏幕上显示的样子。

I've enabled "Screen Support" on my AndroidManifest file. 我在AndroidManifest文件中启用了“屏幕支持”。 The part I'm having trouble with is the values folders. 我遇到麻烦的部分是值文件夹。

These are currently what I have there: (I haven't made any modifications of my own to the folder structure, that I know of) 目前,这些是我所拥有的:(据我所知,我尚未对文件夹结构进行任何修改)

res
-Values
--dimens.xml
--styles.xml
--strings.xml
-Values-v11
--styles.xml
-Values-v14
--styles.xml
-Values-w820dp
--dimens.xml

Do I just make a new values folder for ldpi, hdpi, xhdpi, xxhdpi, etc.? 我是否只是为ldpi,hdpi,xhdpi,xxhdpi等创建新的值文件夹? Then make a new dimens.xml for each? 然后为每个文件创建一个新的dimens.xml? What about after that? 那之后呢?

Here's my AndroidManifest.xml file (Application tag minimized): 这是我的AndroidManifest.xml文件(最小化了Application标签):

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.suckatprogramming.coach"
        android:versionCode="1"
        android:versionName="1.0" >

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

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="19" />

        <application... />

    </manifest>

I'm using a LinearLayout if that means anything. 我正在使用LinearLayout,如果这意味着任何东西。 Let me know if you need anything else from me. 让我知道您是否还有其他需要。 Thanks for any assistance you can provide. 多谢您提供的协助。

I'm not sure whether I get the question , but you can try this.Starting with quite big size for the text in the textvew and to decrease it , until it fits.. 我不确定是否收到问题,但是您可以尝试一下。从textvew中较大的文本开始,然后减小它直到合适为止。

` `

public static void setTextSizeToFitTextView(TextView tv) 



    /*
     * Resize the text size with specified width and height
     * @param width
     * @param height
     */

    int height = tv.getHeight();
    int width = tv.getWidth();
    float mTextSize = tv.getTextSize();
    CharSequence text = tv.getText();
    // Do not resize if the view does not have dimensions or there is no
    // text
    if (text == null || text.length() == 0 || height <= 0 || width <= 0
            || mTextSize == 0) {
        return;
    }

    // Get the text view's paint object
    TextPaint textPaint = tv.getPaint();

    float targetTextSize = 50.0f;

    // Get the required text height
    int textHeight = getTextHeight(text, textPaint, width, targetTextSize);

    // Until we either fit within our text view or we had reached our min
    // text size, incrementally try smaller sizes

    while (textHeight >= height) {
        targetTextSize = targetTextSize - 2;
        textHeight = getTextHeight(text, textPaint, width, targetTextSize);
    }
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
    // textPaint.setTextSize(targetTextSize);
}

// Set the text size of the text paint object and use a static layout to
// render text off screen before measuring
private static int getTextHeight(CharSequence source,
        TextPaint originalPaint, int width, float textSize) {
    // Update the text paint object
    TextPaint paint = new TextPaint(originalPaint);
    paint.setTextSize(textSize);
    // Measure using a static layout
    StaticLayout layout = new StaticLayout(source, paint, width,
            Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    return layout.getHeight();
}`

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

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