簡體   English   中英

適用於不同屏幕的Android文本大小

[英]Android Text Sizes for different 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文件(最小化了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>

我正在使用LinearLayout,如果這意味着任何東西。 讓我知道您是否還有其他需要。 多謝您提供的協助。

我不確定是否收到問題,但是您可以嘗試一下。從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