繁体   English   中英

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

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

根据这部影片

如果要创建一些元素,则在具有不同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>

问题:

为什么电话上的线高不匹配?

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

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

你会用

sp表示字体大小

在其他任何地方都可以输入代码。

浸== DP

在此处输入图片说明

DP

密度无关像素-基于屏幕物理密度的抽象单位。 这些单位是相对于160 dpi屏幕的,因此1 dp是160 dpi屏幕上的一个像素。 dp与像素的比率将随屏幕密度而变化,但不一定成正比。 注意:尽管“ dp”与“ sp”更一致,但编译器接受“ dip”和“ dp”。

在此处输入图片说明

推荐这个问题

暂无
暂无

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

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