简体   繁体   中英

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

As shown on video (at 4:52), I must specify dimensions of this element in dp.

But I got this (Redmi Note vs Redmi 1s (android 4.2.2 on both)):

(I use 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.
  • sp is scale-independent pixels.
  • dip is Density-independent pixels.

You would use

sp for font sizes

dip for enter code hereeverything else.

dip==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. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

在此处输入图片说明

Refer this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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