简体   繁体   English

为什么布局甚至在dp Android中使用宽度也会打扰

[英]Why layout is disturbing even using width in dp Android

I have created a demo application in Android using Nexus 9 2048 X 1536 I have added an EditText and assign width as 1000dp it is working fine in Nexus 9 Emulator. 我使用Nexus 9 2048 X 1536在Android中创建了一个演示应用程序,我添加了EditText并将宽度指定为1000dp,在Nexus 9仿真器中可以正常使用。 But When I change the Emulator as Nexus 5X 1080 X 1920 to test the dp purpose, Then the EditText goes out of the layout and shown about half of it. 但是,当我将仿真器更改为Nexus 5X 1080 X 1920来测试dp目的时,EditText就会超出布局并显示大约一半。

Why it is happening even I assign the width in dp. 为什么会发生,即使我在dp中指定宽度也是如此。 It should adjust the width by screen-resolution. 它应该根据屏幕分辨率调整宽度。

It should adjust the width by screen-resolution. 它应该根据屏幕分辨率调整宽度。

No, it does not. 不,不是的。 dp will adjust the width by screen density , not screen resolution . dp将根据屏幕密度而不是屏幕分辨率来调整宽度。 You asked for it to be 1000dp wide, and so it will be 1000dp wide, regardless of screen resolution or screen size. 您要求它为1000dp宽,因此无论屏幕分辨率或屏幕大小如何,它都将为1000dp宽。

You have a few alternatives: 您有几种选择:

1) The right and recommended one is to make different layouts for different screens. 1)正确的建议之一是针对不同的屏幕进行不同的布局。 For this alternative see Supporting multiple screens 有关此替代方法,请参阅支持多个屏幕

2) Another alternative I used to use before ConstraintLayout was to make everything proportional. 2)我在ConstraintLayout之前使用的另一个替代方法是使所有内容成比例。 When the app starts I take the meassurement of the Display and layout everything based on that. 当应用启动时,我将对Display进行测量并基于此进行所有布局。 This is done mostly programatically not using the xml layout file. 这大部分是通过编程方式完成的,而不使用xml布局文件。

3) Use the new ConstraintLayout from the support library. 3)使用支持库中的新ConstraintLayout。 Using this layout you can constraint your widgest relative to each other and or to guidelines you can add to your layout to delimit sections of it. 使用此布局,您可以限制彼此之间的联系,也可以约束可以添加到布局中以分隔其各个部分的准则。 Your widgets that need to adapt will mostly have a width of 0dp or wrap_content and the constraints will take care of resizig them accordingly. 您需要调整的窗口小部件的宽度大多为0dp或wrap_content,约束将相应地调整它们的大小。

Using ConstraintLayout 使用ConstraintLayout

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

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