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

时间:2014-06-20 19:50:05

标签: android text screens

嘿,我知道有很多关于这个主题的问题,我只需要澄清一下。

我正在努力让我的应用在所有设备尺寸上看起来都一样。我使用'sp'作为“textSize”的值类型,但它似乎没有做任何事情。它在ldpi和hdpi屏幕尺寸上看起来太大了,在mdpi和xxhdpi上太小了。看起来我喜欢看hdpi和xhdpi屏幕。

我在AndroidManifest文件中启用了“屏幕支持”。我遇到问题的部分是值文件夹。

这些是我现在所拥有的: (我没有对文件夹结构进行任何修改,我知道)

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

我是否只为ldpi,hdpi,xhdpi,xxhdpi等创建了一个新的值文件夹?然后为每个创建一个新的dimens.xml?那之后呢?

这是我的AndroidManifest.xml文件(应用程序标记最小化):

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

如果这意味着什么,我正在使用LinearLayout。如果您需要我的任何其他信息,请告诉我。感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

我不确定我是否得到了这个问题,但是你可以尝试一下。开始使用相当大的文本中的文本并减少它,直到它适合..

`

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();
}`