2

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

我试图让我的应用在所有设备尺寸上看起来都一样。我使用“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。让我知道您是否需要我的其他任何东西。感谢您提供的任何帮助。

4

1 回答 1

0

我不确定我是否得到了这个问题,但你可以试试这个。从 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();
}`
于 2014-06-20T20:08:40.367 回答