2014-01-22 56 views
0


幫助理解請。 我需要在特定的框中顯示。 我不喜歡這樣寫道:TextView在不同的顯示器上

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/fon" 
    tools:context=".MainActivity" > 

.... 

<TextView 
     android:id="@+id/textView3" 
     android:layout_width="200dp" 
     android:layout_height="210dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:includeFontPadding="false" 
     android:lineSpacingMultiplier="0.8" 
     android:textAlignment="center" 
     android:textColor="@color/black" 
     android:textSize="20sp" /> 

.... 
</RelativeLayout> 

一切工作在四個英寸屏幕很好,但如果有一個大對角,這將是糟糕的。
隨着屏幕的增加,textView的大小不會改變。
我使用「dp」和「sp」,而不是靜態「px」,但不起作用... 爲什麼會發生這種情況?

截圖:

4 inch

5.4 inch

+0

嘗試使用浸漬(顯示獨立的像素)。 – fedorSmirnov

回答

1

您可以爲不同的屏幕尺寸和密度創建不同的資源目錄,然後在每個屏幕上創建dimens.xml,以提供要使用的文本大小o n個特定的屏幕尺寸,例如:

RES /值-sw420dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <dimen name="font_size">26sp</dimen> 
</resources> 

RES /值-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <dimen name="font_size">30sp</dimen> 
</resources> 

RES /values-sw720dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <dimen name="font_size">36sp</dimen> 
</resources> 

然後調用它在你的佈局文件:

android:textSize="@dimen/font_size" 

這是文字大小的解決方案,但你可以做同樣的事,寬度和高度。

更多資源:

+0

謝謝!!!非常有用的信息 – HelloWorld

+0

不客氣。 –

0

文本視圖大小是靜態的,因爲你已經定義了一個固定的大小,以them.Use包的內容。

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
+0

如果使用「wrap_content」,則文本將佔據屏幕上的所有位置,並且它必須位於某個正方形 – HelloWorld

+0

看起來像您的帖子在特定時間獲得更多連續upvotes。 – Steve

0

你應該調整的TextView的android:layout_width和android:layout_height到

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

如果你想TextView的白背景內線中鋒,你應該添加另一個屬性

android:layout_centerInParent="true" 
+0

如果使用「wrap_content」,文本將佔據屏幕上的所有位置,並且它有必要在某個方格內 – HelloWorld