简体   繁体   English

Android布局缩放

[英]Android layout scaling

If you set your target SDK version in the Android manifest file to "3", then your layout on large-screen devices will be just a scaled up version of what it is on small-screen devices. 如果您将Android清单文件中的目标SDK版本设置为“3”,那么大屏幕设备上的布局将只是小屏幕设备上的缩放版本。 Once you set the target SDK to a higher SDK, you can create separate layouts for each screen density, which provides a much better user experience. 将目标SDK设置为更高的SDK后,您可以为每个屏幕密度创建单独的布局,从而提供更好的用户体验。

However , my layout is mainly made up of images, that are high enough resolution that they display just fine on all screen sizes, even when scaled up, because they're big enough that they don't need to be stretched. 但是 ,我的布局主要由图像组成,这些图像的分辨率足够高,即使按比例放大也能在所有屏幕尺寸上显示得很好,因为它们足够大,不需要拉伸。 Now, it would be much easier for me to just create a small-screen layout and have it scaled up, because it would still look nice on large screens. 现在,我可以更轻松地创建一个小屏幕布局并将其放大,因为它在大屏幕上看起来仍然很好看。 Is there any way I can get that effect without going back to API level 3? 有没有什么方法可以在不回到API级别3的情况下获得该效果?

You can use weight parameter. 您可以使用重量参数。
“weight” is a term that's used to specify how much of the screen a particular view should occupy if there's any room left after it's drawn. “权重”是一个术语,用于指定特定视图在绘制后留下任何空间时应占用多少屏幕。

Simply make a LinearLayout and place two TextViews within it as such: 只需创建一个LinearLayout并在其中放置两个TextViews

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="small"
    android:layout_weight="0.2"
    android:background="#123" />
 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
    android:text="huge"
    android:background="#456"
 />
</LinearLayout>

You will notice how views occupy space accordingly. 您将注意到视图如何占据空间。 You can create any layout you want for smaller screen and specify weight attribute and every thing will be adjusted beautifully 您可以为较小的屏幕创建任何所需的布局并指定权重属性,并且每个项目都将进行精美调整

Just don't specify a layout for larger screens. 只是不要为更大的屏幕指定布局。 By default it uses the same one for all screens unless a more specific layout is available. 默认情况下,除非有更具体的布局,否则它对所有屏幕使用相同的布局。 If you're using match_parent for the width and height and the images to scaleType="fitCenter" or "centerCrop" -- things like that, it should fill whatever screen size that it is run on. 如果您使用match_parent作为宽度和高度,并且图像使用scaleType="fitCenter""centerCrop" - 这样的话,它应该填充它运行的任何屏幕大小。

Dont'f forget making your app compaible with tablets. 不要忘记让你的应用程序与平板电脑兼容。 You must add 你必须添加

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

in your app manifest. 在您的应用清单中。

Hm. 嗯。 I thought Android already did this as long as you aren't using something like AbsoluteLayout. 我认为只要你没有使用像AbsoluteLayout这样的东西,Android就已经这样做了。 There's a few tricks that can help with the transition. 有一些技巧可以帮助过渡。 Not entirely sure what the question is asking, but I'll give it a shot. 不完全确定问题是什么,但我会试一试。 Does API level 3 scale with aspect ratio or something? API级别3是否与纵横比或其他东西成比例?

I found that most of the UI ugliness going from phone to tablet deals with width. 我发现大多数从手机到平板电脑的UI丑陋处理宽度。 There's logic you can put in to fix the width at a certain res. 您可以使用逻辑来修复某个res的宽度。 Otherwise, the layout looks and acts fine. 否则,布局看起来很好。 There other tricks like having assets sized using different dimens.xml, and having different sets of dimens.xml for different screen sizes and densities. 还有其他一些技巧,比如使用不同的dimens.xml来调整资产大小,并为不同的屏幕大小和密度设置不同的dimens.xml集。

You don't have to have every single dimension in each version. 您不必拥有每个版本中的每个维度。 You just have to have the base version of each value set, and you can set the different dimension in the other files. 您只需拥有每个值集的基本版本,并且可以在其他文件中设置不同的维度。

Only way to resize images through different screen size is to use nine patch. 通过不同屏幕尺寸调整图像大小的唯一方法是使用九个补丁。 Here is more resource on that subject : http://developer.android.com/tools/help/draw9patch.html 以下是该主题的更多资源: http//developer.android.com/tools/help/draw9patch.html

I believe this is only thing that you can do is in layout use relative width/height (weight) and images formatted in ninepath. 我相信这只是你可以做的事情是布局使用相对宽度/高度(重量)和格式化为九路径的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM