dp中的高度和宽度在不同设备上看起来不同(Android)

时间:2015-02-27 11:06:56

标签: android

根据此video

如果我想创建一些元素,在具有不同dpi(手机,平板电脑,电视)的设备上看起来必须相同。

如视频所示(4:52),我必须在dp中指定此元素的尺寸。

但我得到了这个(Redmi Note vs Redmi 1s(两者都是android 4.2.2)):

(我使用IntelliJ Idea 14)

以下是我的活动代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              tools:context=".MainActivity">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="60sp"/>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#ff080808">

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="New Button"/>

</LinearLayout>

问题:

为什么手机线路高度不匹配?

为什么小屏幕上的按钮较小?

1 个答案:

答案 0 :(得分:0)

  • px是一个像素。
  • sp是与比例无关的像素。
  • dip是与密度无关的像素。
  

你会用

sp表示字体大小

在其他地方输入代码。

浸== DP

enter image description here

<强> DP

  

与密度无关的像素 - 一种基于密度的抽象单位   屏幕的物理密度。这些单位相对于160 dpi   屏幕,所以一个dp是160 dpi屏幕上的一个像素。比例   dp-to-pixel将随屏幕密度而变化,但不一定如此   按比例分配。注意:编译器同时接受“dip”和“dp”,   虽然“dp”与“sp”更为一致。

enter image description here

Refer this question

相关问题