文字大小和不同的Andr​​oid屏幕尺寸 [英] Text size and different android screen sizes

查看:124
本文介绍了文字大小和不同的Andr​​oid屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,它已经讨论过1000次,但我不能调整文字大小不同的屏幕尺寸。我尝试使用SP的户型单位在我的自定义样式:

I know, it was discussed already 1000 times, but I can't adjust the text size for different screen sizes. I try to use 'sp' as size units in my custom style:

<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
    ...
    <item name="android:textSize">30sp</item>
    ...
</style>

在2.7 QVGA它看起来不错:

In 2.7 QVGA it looks OK:

但在7英寸WSVGA它看起来是这样的:

But in 7in WSVGA it looks like this:

我试图用两个'SP'和'DP'具有相同的结果。

I've tried to use both 'sp' and 'dp' with the same result.

能否请你解释一下如何使这些按钮看起来是一样的任何屏幕上?

Could you please explain how to make these buttons look the same on any screen?

完整的自定义按钮样式

<style name="CustumButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/custom_button</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">30sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">2</item>
</style>

和我的应用程序的主题我有

And in my application theme I have

<item name="android:buttonStyle">@style/CustumButtonStyle</item>

和有我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:background="@drawable/grid"
android:gravity="center"
android:orientation="vertical" android:layout_height="fill_parent">

<Button
    android:id="@+id/buttonContinue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/continue_game" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>



<Button
    android:id="@+id/buttonNewGame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonContinue"
    android:layout_alignRight="@+id/buttonContinue"
    android:layout_below="@+id/buttonContinue"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/new_game" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>



<Button
    android:id="@+id/ButtonAbout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonNewGame"
    android:layout_alignRight="@+id/buttonNewGame"
    android:layout_below="@+id/buttonNewGame"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="@string/about" android:layout_marginTop="3dp" android:layout_marginBottom="3dp"/>

推荐答案

@forcelain我认为你需要检查这个的谷歌IO PDF阅读器为设计。在此PDF转到页号:77中,你会发现怎么会有提示使用dimens.xml为Android。例如不同的设备请参阅下面的结构:

@forcelain I think you need to check this Google IO Pdf for Design. In that pdf go to Page No:77 in which you will find how there suggesting for using dimens.xml for different devices of android for Example see Below structure :

res/values/dimens.xml

res/values-small/dimens.xml

res/values-normal/dimens.xml

res/values-large/dimens.xml

res/values-xlarge/dimens.xml

例如您使用以下dimens.xml的值。

for Example you have used below dimens.xml in values.

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

在其他文件夹中的值,你需要更改值为您的文字大小。

In other values folder you need to change values for your text size .

注意:如由由于Android 3.2支持以下pcated @espinchi小,正常,大和XLARGE已经去$ P $:

Note: As indicated by @espinchi the small, normal, large and xlarge have been deprecated since Android 3.2 in favor of the following:

<一个href="http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts">Declaring平板电脑布局为Android 3.2

对于第一代平板电脑运行Android 3.0,正确的   的方式宣告平板电脑的布局是为了把它们与一个目录   XLARGE配置限定符(例如,资源/布局-XLARGE /)。在   为了容纳其他类型的片剂和屏幕的尺寸,在   特别是,7平板电脑 - 的Andr​​oid 3.2引入了一种新的方式来指定   资源的多个离散的屏幕尺寸。这项新技术是基于   对空间的大小布局需求(如宽度600dp)   而不是试图让您的布局符合广义的尺寸组   (如大的或XLARGE)。

For the first generation of tablets running Android 3.0, the proper way to declare tablet layouts was to put them in a directory with the xlarge configuration qualifier (for example, res/layout-xlarge/). In order to accommodate other types of tablets and screen sizes—in particular, 7" tablets—Android 3.2 introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as large or xlarge).

之所以设计为7平板电脑时使用的是棘手   广义尺寸组是一个7片剂在技术上是在相同的   组作为5手机(大集团)。虽然这两个设备   看似彼此接近的大小,空间为一个量   应用程序的用户界面是显著不同,因为是用户的风格   相互作用。因此,7,5画面不应该总是使用相同的   布局。为了使人们有可能为你提供不同的布局   这两种屏幕,安卓现在允许您指定   基于该宽度和/或高度实际上布局资源   可为您的应用程序的布局,在DP单位指定的。

The reason designing for 7" tablets is tricky when using the generalized size groups is that a 7" tablet is technically in the same group as a 5" handset (the large group). While these two devices are seemingly close to each other in size, the amount of space for an application's UI is significantly different, as is the style of user interaction. Thus, a 7" and 5" screen should not always use the same layout. To make it possible for you to provide different layouts for these two kinds of screens, Android now allows you to specify your layout resources based on the width and/or height that's actually available for your application's layout, specified in dp units.

例如,你设计的布局后,你要使用   平板电脑式的设备,你可能会确定的布局停止   运作良好的时候,屏幕小于600dp宽。这个阈值   因此变成你需要为你的平板电脑布局的最小尺寸。   因此,您现在可以指定,这些资源的布局应该是   用于只有当存在至少可用600dp宽度为您的   应用程序的用户界面。

For example, after you've designed the layout you want to use for tablet-style devices, you might determine that the layout stops working well when the screen is less than 600dp wide. This threshold thus becomes the minimum size that you require for your tablet layout. As such, you can now specify that these layout resources should be used only when there is at least 600dp of width available for your application's UI.

您要么选择一个宽度和设计,以它作为你的最小尺寸,   或者考什么是最小宽度的布局支持,一旦它的   完成了。

You should either pick a width and design to it as your minimum size, or test what is the smallest width your layout supports once it's complete.

请注意:请记住,所有这些新的大小API一起使用的数字   密度无关像素(DP)的价值观和你的布局尺寸   还应始终使用DP单位定义,因为你在乎什么   大约是屏幕空间系统后可用量   占屏密度(相对于使用原始像素   解析度)。有关密度无关像素的更多信息,   阅读条款和概念,较早于本文件。使用新的大小   预选赛

Note: Remember that all the figures used with these new size APIs are density-independent pixel (dp) values and your layout dimensions should also always be defined using dp units, because what you care about is the amount of screen space available after the system accounts for screen density (as opposed to using raw pixel resolution). For more information about density-independent pixels, read Terms and concepts, earlier in this document. Using new size qualifiers

在不同的资源配置,您可以指定基于   可用于您的布局空间概括在表2中。这些   新的预选赛为您提供更多的控制权,具体的屏幕尺寸   您的应用程序支持,相比传统屏幕大小   组(小,中,大,XLARGE)。

The different resource configurations that you can specify based on the space available for your layout are summarized in table 2. These new qualifiers offer you more control over the specific screen sizes your application supports, compared to the traditional screen size groups (small, normal, large, and xlarge).

请注意:您指定使用这些限定的尺寸是不   实际屏幕尺寸。相反,大小在宽度或高度   DP的单位,提供给您的活动的窗口。而Android   系统可能会使用一些屏幕系统的UI(例如系统   条在屏幕或在顶部的状态栏的底部),所以一些   屏幕可能不提供您的布局。因此,大小   声明应具体了解需要由大小   活动 - 系统占所使用的系统UI时的任何空间   声明多少空间,它提供了你的布局。另外要注意,   操作栏被认为是您的应用程序窗口的一部分   空间,虽然你的布局不声明它,所以它减少了   可为您的布局空间,你必须考虑它的   设计。

Note: The sizes that you specify using these qualifiers are not the actual screen sizes. Rather, the sizes are for the width or height in dp units that are available to your activity's window. The Android system might use some of the screen for system UI (such as the system bar at the bottom of the screen or the status bar at the top), so some of the screen might not be available for your layout. Thus, the sizes you declare should be specifically about the sizes needed by your activity—the system accounts for any space used by system UI when declaring how much space it provides for your layout. Also beware that the Action Bar is considered a part of your application's window space, although your layout does not declare it, so it reduces the space available for your layout and you must account for it in your design.

表2.新的配置预选赛(中介绍   安卓3.2)。屏幕配置限定词值说明   smallestWidth swdp

Table 2. New configuration qualifiers for screen size (introduced in Android 3.2). Screen configuration Qualifier values Description smallestWidth swdp

例如:sw600dp sw720dp

Examples: sw600dp sw720dp

在屏幕的基本尺寸,以最短指示   尺寸可用屏幕区域。具体地,该设备的   smallestWidth是最短的屏幕的可用高度和   宽度(你也可以把它当作了最小的宽度为   屏幕)。你可以使用这个限定词,以确保,无论   在屏幕的当前方向,你的应用程序的至少有   宽度DPS可用于它的用户界面。

The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for its UI.

例如,如果您的布局要求其最小尺寸   屏幕区域至少有600 DP在任何时候,那么你可以使用这个   预选赛以创建布局资源,RES /布局sw600dp /。该   系统将利用这些资源,只有当的最小尺寸   可用的屏幕是至少600dp,不管是否600dp   侧是用户感知的高度或宽度。该smallestWidth是   该器件的固定屏幕尺寸特性;该设备的   当屏幕的方向变化smallestWidth不会改变。

For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifier to create the layout resources, res/layout-sw600dp/. The system will use these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes.

设备的smallestWidth考虑到屏幕上的装饰品   和系统UI。例如,如果设备具有一定持久性的UI   该占沿的轴线空间在屏幕上的元素   smallestWidth,系统宣告smallestWidth要小   比实际屏幕尺寸,因为这些屏幕像素不   可为您的用户界面。

The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI.

这是一种替代广义屏尺寸预选赛   (小,中,大,XLARGE),它允许你定义一个离散   号的有效尺寸可用于您的UI。运用   smallestWidth确定,一般屏幕尺寸,因为有用   宽度是常在设计布局中的驱动因素。一个UI会   常垂直滚动,但对相当硬约束   最小的空间,它需要水平。可用宽度也是   在确定是否使用一个单窗格布局关键因素   手机或用于片剂复合窗格布局。因此,你可能最关心的   关于什么可能的最小宽度将每个设备上。   可用屏幕宽度WDP

This is an alternative to the generalized screen size qualifiers (small, normal, large, xlarge) that allows you to define a discrete number for the effective size available for your UI. Using smallestWidth to determine the general screen size is useful because width is often the driving factor in designing a layout. A UI will often scroll vertically, but have fairly hard constraints on the minimum space it needs horizontally. The available width is also the key factor in determining whether to use a one-pane layout for handsets or multi-pane layout for tablets. Thus, you likely care most about what the smallest possible width will be on each device. Available screen width wdp

例如:w720dp w1024dp

Examples: w720dp w1024dp

指定DP单位的最小宽度可在该资源   应使用定义的值。相应系统的   值宽度发生变化时,屏幕的方向切换   横向和纵向之间以反映当前的实际宽度   这是适用于您的用户界面。

Specifies a minimum available width in dp units at which the resources should be used—defined by the value. The system's corresponding value for the width changes when the screen's orientation switches between landscape and portrait to reflect the current actual width that's available for your UI.

这常常是有用的确定是否使用多块窗格布局,   因为即使在一个平板设备,你经常会不希望同样的   为纵向的多面板布局,你做的景观。   因此,你可以用它来指定所需的最小宽度   布局,而不是使用两个屏幕大小和方向   预选赛在一起。可用屏幕高度HDP

This is often useful to determine whether to use a multi-pane layout, because even on a tablet device, you often won't want the same multi-pane layout for portrait orientation as you do for landscape. Thus, you can use this to specify the minimum width required for the layout, instead of using both the screen size and orientation qualifiers together. Available screen height hdp

例如:h720dp h1024dp等。

Examples: h720dp h1024dp etc.

指定DP单位的最低屏幕高度,在其资源   应使用定义的值。相应系统的   当屏幕的方向转换价值的高度变化   横向和纵向之间以反映当前的实际高度   这是适用于您的用户界面。

Specifies a minimum screen height in dp units at which the resources should be used—defined by the value. The system's corresponding value for the height changes when the screen's orientation switches between landscape and portrait to reflect the current actual height that's available for your UI.

使用这种方法来定义你的布局所需的高度是非常有用   相同的方式WDP是代替用于限定所要求的宽度,   同时使用屏幕大小和方向限定符。然而,大多数   应用程序不需要这个限定词,考虑到用户界面常常滚动   垂直并因此更加灵活与多少高度是   可用,而宽度是更硬。

Using this to define the height required by your layout is useful in the same way as wdp is for defining the required width, instead of using both the screen size and orientation qualifiers. However, most apps won't need this qualifier, considering that UIs often scroll vertically and are thus more flexible with how much height is available, whereas the width is more rigid.

在使用这些限定词似乎比使用更复杂   屏幕尺寸组,它实际上应该是简单的,一旦你确定   要求你的UI。当你设计你的用户界面,更主要的   你可能关心的是实际尺寸为您的应用程序   手机风格的用户界面和使用平板式的用户界面之间切换   多个窗格。这种开关的确切地点将取决于你的   特殊的设计,也许你需要为你的平板电脑布局720dp宽度,   也许600dp就够了,或者480dp,或它们之间的一些数字。运用   这些限定词表2,你是在precise大小的控制   它的布局的变化。

While using these qualifiers might seem more complicated than using screen size groups, it should actually be simpler once you determine the requirements for your UI. When you design your UI, the main thing you probably care about is the actual size at which your application switches between a handset-style UI and a tablet-style UI that uses multiple panes. The exact point of this switch will depend on your particular design—maybe you need a 720dp width for your tablet layout, maybe 600dp is enough, or 480dp, or some number between these. Using these qualifiers in table 2, you are in control of the precise size at which your layout changes.

有关这些大小配置预选赛的详细讨论,请参见   提供了资源文件。配置实例

For more discussion about these size configuration qualifiers, see the Providing Resources document. Configuration examples

要帮助你定位你的一些设计适用于不同类型的   设备,这里有一些数字为典型的屏幕宽度:

To help you target some of your designs for different types of devices, here are some numbers for typical screen widths:

320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7" tablet (600x1024 mdpi).
720dp: a 10" tablet (720x1280 mdpi, 800x1280 mdpi, etc).

     

使用大小预选赛从表2,您的应用程序可以切换   你的不同的布局资源,使用手机和平板电脑之间的   任何数量的要为宽度和/或高度。例如,如果是600dp   最小的可用宽度支持平板电脑的布局,你可以   提供这两套布局:

Using the size qualifiers from table 2, your application can switch between your different layout resources for handsets and tablets using any number you want for width and/or height. For example, if 600dp is the smallest available width supported by your tablet layout, you can provide these two sets of layouts:

RES /布局/ main_activity.xml#对于手机   RES /布局sw600dp / main_activity.xml#对于片

res/layout/main_activity.xml # For handsets res/layout-sw600dp/main_activity.xml # For tablets

在这种情况下,可用的屏幕空间的最小宽度必须   600dp为了使片剂布局被应用

In this case, the smallest width of the available screen space must be 600dp in order for the tablet layout to be applied.

有关要在其中进一步定制用户界面的其他案件   大小如7和10平板电脑区分开来,你可以定义   额外的最小宽度的布局:

For other cases in which you want to further customize your UI to differentiate between sizes such as 7" and 10" tablets, you can define additional smallest width layouts:

RES /布局/ main_activity.xml#对于手机(小于   600dp可用宽度)RES /布局sw600dp / main_activity.xml#对于7   片(600dp宽更大)RES /布局sw720dp / main_activity.xml

res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7" tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml

请注意,previous两套例如资源的使用   最小宽度限定符,swdp,指定最小的   在屏幕的两侧,而不管设备当前的   方向。因此,使用swdp是指定一个简单的方法   整体屏幕尺寸可供布局忽略屏幕的   方向。

Notice that the previous two sets of example resources use the "smallest width" qualifier, swdp, which specifies the smallest of the screen's two sides, regardless of the device's current orientation. Thus, using swdp is a simple way to specify the overall screen size available for your layout by ignoring the screen's orientation.

然而,在某些情况下,可能是什么重要的布局是   到底有多少宽度或高度为当前可用。例如,   如果你有两个片段并排双面板布局,你   可能要使用它时,屏幕上提供至少600dp的   宽度,该设备是在横向或纵向方向。在   这种情况下,你的资源可能是这样的:

However, in some cases, what might be important for your layout is exactly how much width or height is currently available. For example, if you have a two-pane layout with two fragments side by side, you might want to use it whenever the screen provides at least 600dp of width, whether the device is in landscape or portrait orientation. In this case, your resources might look like this:

RES /布局/ main_activity.xml#对于手机(小于   600dp可用宽度)RES /布局w600dp / main_activity.xml#   多面板

res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-w600dp/main_activity.xml # Multi-pane (any screen with 600dp available width or more)

请注意,第二组使用的是可用宽度预选赛中,   WDP。以此方式,一个设备可以实际上使用两种布局,这取决于   在屏幕的取向(如果可用的宽度为至少   600dp在一个取向和小于600dp在其他   取向)。

Notice that the second set is using the "available width" qualifier, wdp. This way, one device may actually use both layouts, depending on the orientation of the screen (if the available width is at least 600dp in one orientation and less than 600dp in the other orientation).

如果可用高度是给你一个问题,那么你可以做同样的   使用HDP预选赛。或者,即使结合WDP和HDP   预选赛,如果你需要,真正做到具体。

If the available height is a concern for you, then you can do the same using the hdp qualifier. Or, even combine the wdp and hdp qualifiers if you need to be really specific.

这篇关于文字大小和不同的Andr​​oid屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆