2014-01-19 126 views
1

我正在開發一款android應用程序,其中屏幕頂部有3個按鈕,我使用layout_weight屬性定義了它們。這些按鈕適用於包括平板電腦在內的所有屏幕尺寸,但其文字不適用Android - 所有屏幕尺寸的按鈕文本大小

這是它看起來像一個關係5

這是它看起來像一個關係10

見區別?
在nexus 5上,它很清晰,但在nexus 10上幾乎看不到任何東西!

這裏是我的XML

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="Button 1" 
     android:layout_weight="1" 
     android:id="@+id/button" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="Button 2" 
     android:layout_weight="1" 
     android:id="@+id/button2" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="Button 3" 
     android:layout_weight="1" 
     android:id="@+id/button3" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:background="#000000" 
    android:layout_weight="1" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

</LinearLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

</LinearLayout> 

那麼,如何讓按鈕上的文字大小在不同的屏幕大小自動調節,而無需進行不同的佈局爲不同的屏幕尺寸?

請幫助我與代碼,因爲我幾乎是一個初學者!

在此先感謝!

+1

[文本大小和不同的android屏幕大小](http:// stackoverflow。com/questions/9877946/text-size-and-different-android-screen-sizes) – StarsSky

+0

@StarsSky我想避免製作不同的文件夾,並尋找解決方案來製作支持所有屏幕尺寸的單一佈局。如果您有解決方案,請幫助我。謝謝! –

+0

不同的佈局文件夾已被引入,以滿足您的要求 – StarsSky

回答

1

您可以在sp中設置按鈕textSize(即:24sp)。

請記住,此值是指mdpi屏幕,它將根據屏幕分辨率放大或縮小。

因此,在ldpi屏幕上,它會縮小到18sp,而在xhdpi屏幕上它會被放大到48sp。

除此之外,您可以在您的values文件夾中的dimens.xml文件中定義維度值(但您必須創建多個值文件夾,每個文件夾對應於您支持的每個分辨率)。

+0

其實我在佈局中有119個按鈕,而24sp對他們來說太大了。我的意思是在xhdpi屏幕上,您的解決方案運行良好,但在小型設備上,它太大了。請幫我解決這個問題。 謝謝! –

+0

要在xhdpi屏幕上獲得24 sp,您的初始值應爲12sp。但是這個很小,在ldpi屏幕上會變成10sp。無論如何,我懷疑你可以在這麼小的屏幕上安裝119個按鈕。無論如何,您可以使用維度解決方案爲每個分辨率設置不同的自定義維度。 –

+0

確定將使用尺寸。作爲初學者,你能否引導我?謝謝! –

3

您目前正在嘗試擴展整個應用程序,而不是構建適當的平板電腦用戶界面。您將按鈕的寬度設置爲按預期工作的屏幕的1/3。您沒有更改按鈕的textSize,因此Android會選擇默認的textSizes。默認的textSizes是一個特定的大小,與View的尺寸無關。你不能(也不想)在4「手機和10」平板電腦上擴展完整的用戶界面。

由於您應該在平板電腦上顯示的信息多於在手機上顯示的信息,因此無法在單一佈局中爲手機和平板電腦構建(正確)用戶界面。儘管您可以重新使用大部分佈局。 您可能想要閱讀Android Developers網站上的Supporting Different Screen Sizes培訓。

+0

我之前讀過。基本上我想要做的是使按鈕的文字大小自動適應大屏幕。 –