[英]Android button size changes at different devices (width is set to dp)
[英]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>
問題:
為什么電話上的線高不匹配?
為什么在較小的屏幕上按鈕較小?
你會用
sp表示字體大小
在其他任何地方都可以輸入代碼。
浸== DP
DP
密度無關像素-基於屏幕物理密度的抽象單位。 這些單位是相對於160 dpi屏幕的,因此1 dp是160 dpi屏幕上的一個像素。 dp與像素的比率將隨屏幕密度而變化,但不一定成正比。 注意:盡管“ dp”與“ sp”更一致,但編譯器接受“ dip”和“ dp”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.