简体   繁体   English

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

[英]Height and width in dp looks different on different devices (Android)

According to this video 根据这部影片

If I want create some element, which must looks the same on devices with different dpi (Phone, Tablet, TV). 如果要创建一些元素,则在具有不同dpi的设备(电话,平板电脑,电视)上,它们的外观必须相同。

As shown on video (at 4:52), I must specify dimensions of this element in dp. 如视频所示(在4:52),我必须在dp中指定此元素的尺寸。

But I got this (Redmi Note vs Redmi 1s (android 4.2.2 on both)): 但是我得到了这个(Redmi Note vs Redmi 1s(两个都是Android 4.2.2)):

(I use IntelliJ Idea 14) (我使用IntelliJ Idea 14)

Here is my code from activity: 这是我的活动代码:

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

Questions: 问题:

Why line height is not match on phones? 为什么电话上的线高不匹配?

Why button is smaller on smaller screen? 为什么在较小的屏幕上按钮较小?

  • px is one pixel. px是一个像素。
  • sp is scale-independent pixels. sp是与比例无关的像素。
  • dip is Density-independent pixels. dip是与密度无关的像素。

You would use 你会用

sp for font sizes sp表示字体大小

dip for enter code hereeverything else. 在其他任何地方都可以输入代码。

dip==dp 浸== DP

在此处输入图片说明

dp DP

Density-independent Pixels - an abstract unit that is based on the physical density of the screen. 密度无关像素-基于屏幕物理密度的抽象单位。 These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. 这些单位是相对于160 dpi屏幕的,因此1 dp是160 dpi屏幕上的一个像素。 The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. dp与像素的比率将随屏幕密度而变化,但不一定成正比。 Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp". 注意:尽管“ dp”与“ sp”更一致,但编译器接受“ dip”和“ dp”。

在此处输入图片说明

Refer this question 推荐这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM