簡體   English   中英

如果我根據特定手機設置字體大小,如何根據android中的屏幕大小更改字體大小?

[英]How to change the font size according to the screen size in android if I set the font sizes according to a specific phone?

我在 .xml 文件中設置了所有字體大小,以便它們在特定手機(准確地說是 iPhone 11)上正確顯示。 如何根據個別手機的屏幕大小更改字體大小?

對於 Android 中的字體,強烈建議使用sp

對於文本視圖,您可能需要查看Android TextView AutoSizing

使用這個庫,它非常令人印象深刻並且支持幾乎所有分辨率並且非常有效。 在 gradle 中添加這些依賴項並同步 gradle :

優點:非常易於使用,文本大小隨屏幕大小而變化。

將此用於視圖大小(高度、寬度、邊距等)

implementation 'com.intuit.sdp:sdp-android:1.0.6'

用法 :

android:layout_marginBottom="@dimen/_30sdp"

將此用於文本大小:

implementation 'com.intuit.ssp:ssp-android:1.0.6'

用法 :

android:layout_marginBottom="@dimen/_14ssp"

您首先需要獲取 iPhone 11 與運行該應用程序的手機之間的屏幕尺寸比例。 在 MainActivity 中定義一個靜態變量:

private static double SCREEN_SIZE_RATIO;

然后獲取 iPhone 11 的屏幕尺寸。 不要使用原始尺寸,因為它們沒有考慮到屏幕沒有覆蓋整個手機的事實。 相反,取像素分辨率並將其除以 ppi。 計算iPhone 11的對角線長度和目標手機的對角線長度,將后者除以前者得到屏幕尺寸比例。

private void calculateScreenSizeRatio() {
    double iphone11Width = ((double) 828) / ((double) 326); // Values from https://www.apple.com/iphone-11/specs/
    double iphone11Height = ((double) 1792) / ((double) 326);
    double iphone11Diagonal = Math.hypot(iphone11Width, iphone11Height);

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    double screenWidthPixels = displayMetrics.widthPixels;
    double screenHeightPixels = displayMetrics.heightPixels;
    double screenWidth = screenWidthPixels / displayMetrics.xdpi;
    double screenHeight = screenHeightPixels / displayMetrics.ydpi;
    double screenDiagonal = Math.hypot(screenWidth, screenHeight);

    SCREEN_SIZE_RATIO = screenDiagonal / iphone11Diagonal;
}

請注意,計算對角線長度時,沒有考慮高寬比,這會帶來輕微的不准確性。

現在,編寫一個靜態函數來獲取字體大小(最初在 .xml 中定義),將其乘以屏幕大小比例,並將結果設置為新的字體大小。

public static void setTextSize(TextView view) {
    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (view.getTextSize() * SCREEN_SIZE_RATIO));
}

getTextSize() 函數返回以像素為單位的大小,這就是我們以像素為單位設置大小的原因(使用 TypedValue.COMPLEX_UNIT_PX)。 記住 EditText 和 Button 都是 TextView 的子類,所以這個函數也可以用於 EditTexts 和 Buttons。

在您的 MainActivity 中,編寫類似於以下內容的函數:

private void setTextSizes() {
    setTextSize(emailEditText);
    setTextSize(passwordEditText);
    setTextSize(logInButton);
    setTextSize(registerTextView);
    setTextSize(invalidTextView);
    setTextSize((TextView) findViewById(R.id.text_view_no_account));
}

在其他活動中,編寫一個類似如下的函數:

private void setTextSizes() {
    MainActivity.setTextSize(amountEditText);
    MainActivity.setTextSize((TextView) findViewById(R.id.edit_text_payment_method));
    MainActivity.setTextSize((TextView) findViewById(R.id.add_button));
    MainActivity.setTextSize(creditDebitCardTextView);
    MainActivity.setTextSize(bankTransferTextView);
}

您可以將任何 TextViews、EditTexts 和 Buttons 放入此函數中。 記住在使用 findViewbyId() 時將 EditTexts 和 Buttons 轉換為 (TextView),就像我在第二個代碼片段中使用edit_text_payment_methodadd_button所做的那樣。

最后在您的每個活動中調用onCreate()中的setTextSizes() ,最好是在您分配了類變量之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM