android–Dp单元不会在不同的屏幕上缩放布局

android–Dp单元不会在不同的屏幕上缩放布局,第1张

概述阅读本文(http://developer.android.com/guide/practices/screens_support.html)后,我使用xml文件中的dp单元开发了一个完整的应用程序.但是,当我在不同的屏幕上测试应用程序时,布局太大或太小.我认为dp单元会为我解决这个问题.为什么不呢?我不想使用权重属性,因为一切都已经完成.

阅读本文(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"        /><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"        /><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"        /><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不是一个真正适合所有解决方案的解决方案.

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

我要做的是为每个支持的屏幕尺寸放置一个dimens.xml文件:

> res / values-small / dimens.xml
> res / values-normal / dimens.xml
> res / values-large / dimens.xml
> res / values-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"    />
总结

以上是内存溢出为你收集整理的android – Dp单元不会在不同的屏幕上缩放布局全部内容,希望文章能够帮你解决android – Dp单元不会在不同的屏幕上缩放布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1108241.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存