DP单位不结垢在不同的屏幕布局 [英] Dp unit does not scale layouts in different screens

查看:191
本文介绍了DP单位不结垢在不同的屏幕布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读完之后( http://developer.android.com/guide/practices/ screens_support.html ),我已经开发使用DP单元的XML文件内的整个应用程序。然而,当我在不同的屏幕上测试应用程序时,布局过大或过小。

After reading this (http://developer.android.com/guide/practices/screens_support.html), I have developed an entire app using the dp unit inside the xml files. However, when I test the app in different screens, the layouts are either too big or too small.

我以为DP单位将解决这个问题对我来说。为什么没有呢?我不想使用权重属性,因为一切都已经完成了。

I thought the dp unit would fix that for me. Why didn't it? I do not want to use the weight attribute since everything is already done.

一个XML布局:
        

One xml layout:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:src="@drawable/logo3"
    android:scaleType="centerCrop"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/select_level"
    android:textColor="#4cb122"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:textSize="20dp"
    android:layout_marginTop="20dp"
    />

<Button
    android:background="@drawable/red_button"
    android:layout_width="200dp"
    android:layout_height="55dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="25dp"
    android:text="@string/easy"
    android:textSize="15dp"
    android:onClick="playEasy"
    style="custom_button"
    />

<Button
    android:background="@drawable/green_button"
    android:layout_width="200dp"
    android:layout_height="55dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:text="@string/medium"
    android:textSize="15dp"
    android:onClick="playMedium"
    style="custom_button"
    />

<Button
    android:background="@drawable/blue_button"
    android:layout_width="200dp"
    android:layout_height="55dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:textSize="15dp"
    android:text="@string/unbeatable"
    android:onClick="playUnbeatable"
    style="custom_button"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginTop="113dp"
    android:textSize="15dp"
    android:textColor="@color/secondTextColor"
    android:text="@string/developed_by"
    />

我能做什么?谢谢!

推荐答案

使用DP的尺寸是不是一个真正的一刀切这个问题的所有方案。

Using dp for dimensions is not a truly one size fits all solution for this problem.

请注意,你应该使用重布局可能时,一般一个DP值应该为所有的屏幕尺寸工作。不过,有时你会运行到导致问题一个边缘的情况下,你只需要做些什么来让它工作(例如我不得不使用这种技术在TabLayout正确选项卡上的定位徽章用于所有屏幕尺寸)。

Note that you should use layout weight when possible, and in general one dp value should work for all screen sizes. However, sometimes you will run in to a edge case that causes problems, and you just need to do something to make it work (For example I had to use this technique for positioning a badge on a tab in a TabLayout correctly for all screen sizes).

我做些什么来解决它是把一个文件dimens.xml为每个支持的屏幕尺寸:

What I do to get around it is to put a dimens.xml file for each supported screen size:


  • RES /值小/ dimens.xml

  • res/values-small/dimens.xml

RES /值正常/ dimens.xml

res/values-normal/dimens.xml

RES /价值观大/ dimens.xml

res/values-large/dimens.xml

RES /价值观XLARGE / dimens.xml

res/values-xlarge/dimens.xml

您可以使用其他预选赛以及定位到平板电脑如果需要,的在这里看到的指南,配置预选赛名的。

You can use other qualifiers as well to target tablets if that is needed, see here for a guide to configuration qualifier names.

然后,指定为在每个文件中的每个画面大小限定符每个维度(注意,这仅需要适用于对非常大或非常小屏幕导致问题的维度值将要执行)。

Then, specify each dimension for each screen size qualifier in each file (note that this only needs to be done for the dimension values that are causing problems on very large or very small screens).

例如在res /价值观大/ dimens.xml你可能有这样的:

For example in res/values-large/dimens.xml you might have this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="image_view_height">140dp</dimen>

</resources>

然后在res /值小/ dimens.xml你可能有这使其适合较小的屏幕上:

Then in res/values-small/dimens.xml you might have this to make it fit on the smaller screens:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="image_view_height">96dp</dimen>

</resources>

然后,在你的布局, @扪/ your_dimens_id 引用它,而框架会选择正确的承担设备的屏幕尺寸:

Then, in your layout, reference it with @dimen/your_dimens_id, and the framework will choose the correct one to take for the screen size of the device:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="@dimen/image_view_height"
    android:src="@drawable/logo3"
    android:scaleType="centerCrop"

    />

这篇关于DP单位不结垢在不同的屏幕布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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