【问题标题】:Will using dp take care of physical screen size?使用 dp 会处理物理屏幕大小吗?
【发布时间】:2015-06-11 13:41:21
【问题描述】:

我阅读了很多张贴在这里的questions,但我的问题没有得到明确的答案。使用单位 dp 会照顾物理屏幕尺寸吗?我已经为我的所有视图使用了单位 dp,并且我试图使它们的大小相对。但即使那样我也必须为其中的一些提供绝对大小。应用程序在 4 英寸和 5 英寸屏幕上运行是否相同。这是结构:

这是我的 XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="400dp" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_above="@+id/relativeLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/left" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/right" />
    </RelativeLayout>

         <RelativeLayout
        android:layout_width="fill_parent"
        android:id="@+id/rel2"
        android:layout_height="350dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout1" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/lc" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="280dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout1"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginLeft="120dp"
                android:layout_marginTop="140dp"
                android:src="@drawable/untitled" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/linearLayout2"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="64dp"
                android:src="@drawable/lc" />

        </LinearLayout>

  </RelativeLayout>

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
     >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/left" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/imageButton1"
        android:layout_alignParentRight="true"
        android:src="@drawable/right" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

</LinearLayout>

提前谢谢....

【问题讨论】:

  • 您可以使用模拟器测试许多不同的屏幕尺寸,何不去看看呢?
  • @HMiller 所有 4 英寸以上的尺寸都比我的显示器支持的分辨率更高。这就是我无法尝试的原因。

标签: android xml screen scaling


【解决方案1】:

您可以使用 values/dimens.xml

如下所示(例如:所有设备均为 3dp)

创建值/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">3dp</dimen>
</resources>

创建值-sw360dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">3.375dp</dimen>
</resources>

创建值-sw480dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">4.5dp</dimen>
</resources>

创建值-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">5.625dp</dimen>
</resources>

创建值-sw720dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="three">6.75dp</dimen>
</resources>

dp的比率

对于默认值文件夹,它的 1

360dp - 1.125

480dp - 1.5

600dp - 1.875

720dp - 2.25

如何使用?示例:@dimen/three

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/three"
    android:layout_above="@+id/relativeLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

【讨论】:

    【解决方案2】:

    不使用 dp 我们无法处理不同的屏幕尺寸。

    为此,您需要使用不同的策略 你可以使用

    android:layout_weight=".5"
    

    如果您设置android:layout_width="0dp",这将覆盖一半屏幕 两个视图在一行中

    如果你设置 height= 0dp 它将覆盖屏幕总高度的一半

    注意

    总重量 = 1 ;你可以按照 0.5 0.3 等进行划分

    【讨论】:

    • 是的,我也想过,但我需要知道我的一个布局在运行时占据的高度(图片中间的小黑球)
    • 好的,我想你想读取那个黑球的当前位置,如果是,那么在相对布局上你可以使用 getX() getY() 获得该视图的 X,Y 你也可以使用 getLeft() 或者你想要高度然后getHeight() 这些功能可以正常工作,没有屏幕大小问题
    • 你想知道球的当前位置吗?
    • 不,实际上我在线性布局中使用左边距和上边距来移动球,所以我知道球在哪里。我必须在屏幕上显示所有视图。
    【解决方案3】:

    我们不知道您的可绘制尺寸如何,因此很难回答您的问题。但正如提到的here

    dp 与屏幕尺寸无关,它与密度有关。你可以有 2 个 5 英寸的屏幕。一个使用 hdpi,第二个使用 xxhdpi,您的布局将重新缩放为可用像素。

    ldpi 是 mdpi 的 0.75 倍

    hdpi 是 mdpi 的 1.5 倍

    xhdpi 是 mdpi 的 2 倍尺寸

    【讨论】:

      【解决方案4】:

      您可能需要采用不同的策略。首先你可以使用dimens文件来定义不同屏幕密度的布局或者使用layout_weight。

      我建议您查看 Android 开发人员指南,了解针对不同屏幕尺寸设计布局:https://developer.android.com/training/multiscreen/index.html

      【讨论】:

        猜你喜欢
        • 2014-01-10
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-03-17
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        相关资源
        最近更新 更多