2014-01-22 49 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