Android支持多屏幕(3)Declaring Tablet Layouts for Android 3.2


Declaring Tablet Layouts for Android 3.2

转载请注明:http://blog.csdn.net/liaoqianchuan00/article/details/23673489

翻译自:http://developer.android.com/guide/practices/screens_support.html

 


在第一代运行Android 3.0的平板上,正确的声明layout的方式是把这些layout文件放到xlarge限定符下面(比如res/layout-xlarge/)。为了适应其他种类的平板和屏幕大小-比如7英寸平板-Android3.2引入了一种新的方式来限定更精确的屏幕大小。这个新技术是基于你的layout对空间的需求的(比如600dp的宽度),而不是像以前一样用广义的屏幕大小来定义(比如large或者xlarge)。

 

这样做的原因是7英寸的平板有的时候其实是和5英寸的手机在广义上都是术语large的(可以查看之前定义屏幕大小large和xlarge等得区间范围)。这个时候,虽然他们都属于同一个广义的屏幕大小,但是实际上他们的UI是需要有不同设计的。因此,7英寸和5英寸的屏幕不总是用一样的layout。为了让你能对这两个屏幕使用不同的UI,Android允许你基于屏幕的宽度和高度来定义你自己的layout,使用dp单位。

 

比如,在你为平板风格的设备设计了layout后,你不想让这个layout使用在屏幕宽度小于600dp的屏幕上。这个值就是你的平板layout限定的最小值。这样,你的layout就只能用在那些屏幕宽度小于600dp的屏幕上。

 

你可以选择一个宽度,并且根据这个宽度来设计layout,或者设计完成后测试你的layout支持的最小宽度。

 

注意:这些宽度都是基于dp定义的,并且你应该在你的layout中用dp来定义大小,因为你关心的是在系统在计算屏幕密度后的空间(而不是基于像素点的)。

 

使用新的大小限定符

这些基于屏幕空间大小的限定符可以在下表中找到。这些限定符相比以前的广义的屏幕大小限定符能更好的控制一些特别的屏幕大小。

 

注意:你使用的这些限定符不是屏幕实际的大小,而是以dp为单位的你的activity的界面的宽度和高度。Android系统可能会使用一部分屏幕空间来作系统的UI(比如屏幕下端的system bar或者屏幕上端的status bar)。所以屏幕的一些空间对于你的layout实际上可能是不可用的。所以你限定的这些大小应该是你Activity需要使用的大小。同时需要注意的是Action Bar被认为是你的程序窗口的一部分,所以你在设计你的layout的时候应该摆这部分考虑进去。

Screen configuration

Qualifier values

Description

smallestWidth

sw<N>dp

Examples:
sw600dp
sw720dp

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 <N> dps of width available for it UI.

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 qualifer 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.

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.

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

w<N>dp

Examples:
w720dp
w1024dp

Specifies a minimum available width in dp units at which the resources should be used—defined by the <N> 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.

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

h<N>dp

Examples:
h720dp
h1024dp
etc.

Specifies a minimum screen height in dp units at which the resources should be used—defined by the <N> 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.

Using this to define the height required by your layout is useful in the same way as w<N>dp 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的需求之后这些方法更简单。你可能最关心的事情是那种情况下你的程序将会在handset-style和tablet-style UI之间切换。这些取决于你的限定-你可能需要720dp的宽度来显示平板layout,也可能600dp就足够了,也可能480dp,或者一些介于这些数字之间的值。使用这些新的限定符能让你更好的控制你layout的切换。

 

实例

为了帮助你为不同的设备设计不同的layout,这里列出了一些典型的屏幕宽度:

l   320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800hdpi, etc).

l   480dp: a tweener tablet like the Streak (480x800 mdpi).

l   600dp: a 7” tablet (600x1024 mdpi).

l   720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

 

使用上面表中得限定符,你可以很好的控制平板和手机的layout。比如,你的平板layout支持的最小宽度是600dp,你可以定义下面两种layout:

 

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

 

这个例子中,你平板layout支持的最小屏幕宽度是600dp。

 

又比如,你想要为7英寸和10英寸平板设计不同的layout,你可以用下面的方式定义layout:

 

res/layout/main_activity.xml          # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide andbigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide andbigger)

 

我们可以看到之前的这两个例子都使用了“smallest width”限定符,sw<N>dp,限定了屏幕的宽度而不关心屏幕的旋转方向。所以,在不关心屏幕方向的情况下使用sw<N>dp是最简单的方式。smallest width在屏幕方向旋转的时候是不会改变的)

 

但是,有的时候,对你的layout很重要的是当前的宽度和高度。比如,如果你有一个two-panel的layout,分别有两个fragment,你可能想要程序只要在屏幕可用宽度大于600dp的时候就用这个layout。所以你的资源文件看起来会比较像下面这种:

 

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

 

我们注意到第二种布局用得时“availablewidth限定符,w<N>dp.用这种方式,一个设备可能会同时用到这两种layout,这取决于屏幕的方向(如果在一个方向可用宽度至少600dp而另外一种屏幕方向小于600dp)。

 

如果你需要考虑高度的话,你可以同样使用h<N>dp限定符。甚至于你可以将w<N>dp和h<N>dp连起来一起用。

 

Declaring screen size support

一旦你完成了针对不同屏幕大小的不同layout,在manifest文件中声明你的程序支持的屏幕类型同样重要。

和新的限定符一起,Android3.2引入了manifest中<supports-screens>的一些新属性:

 

1.       android:requiresSmallestWidthDp

规定了最低屏幕宽度。smallestWidth是可被你的程序使用的最小屏幕空间。所以一个设备如果要兼容你的程序,他得smallestWidth必须要等于或者大于这个值(通常,你提供的这个layout支持的“small width”值与屏幕方向没有关系)。

 

比如,如果你的程序只支持至少600dp可用宽度的平板设备:

 

<manifest ... >
    <supports-screensandroid:requiresSmallestWidthDp="600" />
    ...
</manifest>

 

但是,如果你的程序支持所有的Android设备,那么你不需要声明这个属性。

注意:你的程序在运行的时候系统并不关心这个属性,这个只是用来在像GooglePlay这种应用程序服务器筛选程序的时候用。但是GooglePlay现在还补支持这个属性,所以你还需要继续使用其他的属性来让你的程序不支持小屏幕。

 

2.       android:compatibleWidthLimitDp

This attribute allows you to enable screencompatibility mode as a user-optional feature by specifying themaximum "smallest width" that your application supports. If thesmallest side of a device's available screen is greater than your value here,users can still install your application, but are offered to run it in screencompatibility mode. By default, screen compatibility mode is disabled and yourlayout is resized to fit the screen as usual, but a button is available in thesystem bar that allows users to toggle screen compatibility mode on and off.

Note: If your application's layoutproperly resizes for large screens, you do not need to use this attribute. Werecommend that you avoid using this attribute and instead ensure your layoutresizes for larger screens by following the recommendations in this document.

3.       android:largestWidthLimitDp

This attribute allows you to force-enable screencompatibility mode by specifying the maximum "smallestwidth" that your application supports. If the smallest side of a device'savailable screen is greater than your value here, the application runs inscreen compatibility mode with no way for the user to disable it.

Note: If your application's layoutproperly resizes for large screens, you do not need to use this attribute. Werecommend that you avoid using this attribute and instead ensure your layoutresizes for larger screens by following the recommendations in this document.

 

注意:当在开发Android3.2或者更高版本的程序的时候,你不能用将新的这些属性和老的screensize属性一起用,那样可能会导致一些不可预期的问题。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值