Dp单元不会在不同屏幕中缩放布局

时间:2016-02-06 18:58:32

标签: android xml

阅读完本文(http://developer.android.com/guide/practices/screens_support.html)之后,我使用xml文件中的dp单元开发了一个完整的应用程序。但是,当我在不同的屏幕上测试应用程序时,布局太大或太小。

我认为dp单元会为我解决这个问题。为什么不呢?我不想使用权重属性,因为一切都已经完成。

一个xml布局:         

<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"
    />

我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:5)

对于这个问题,使用dp并不是一个真正适合所有解决方案。

请注意,您应尽可能使用布局权重,通常一个dp值应适用于所有屏幕尺寸。但是,有时您会遇到导致问题的边缘情况,并且您只需要做一些事情来使其工作(例如,我必须使用此技术在TabLayout的选项卡上正确定位徽章以适用于所有屏幕尺寸)。

我要解决的问题是为每个支持的屏幕尺寸添加一个dimens.xml文件:

  • RES /值小/ dimens.xml

  • RES /值正常/ dimens.xml

  • RES /值-大/ dimens.xml

  • RES /值-XLARGE / dimens.xml

如果需要,您还可以使用其他限定符定位平板电脑see here for a guide to configuration qualifier names

然后,为每个文件中的每个屏幕大小限定符指定每个维度(请注意,只需要在非常大或非常小的屏幕上导致问题的维度值进行此操作)。

例如在res / values-large / dimens.xml中,你可能会这样:

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

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

</resources>

然后在res / values-small / dimens.xml中你可能会让它适合较小的屏幕:

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

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

</resources>

然后,在您的布局中,使用@dimen/your_dimens_id引用它,框架将选择正确的设备屏幕尺寸:

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

    />
相关问题