简体   繁体   English

如何为所有屏幕尺寸制作灵活的 UI?

[英]How to make a flexible UI for all screen sizes?

How can I make my ui look the same in different screens?如何让我的 ui 在不同屏幕上看起来相同?

I made an app and tested it to run on 2 different phones with different screen sizes and it appeared very differently.我制作了一个应用程序并测试它可以在 2 部不同屏幕尺寸的不同手机上运行,​​它看起来非常不同。

Here are some screenshots:以下是一些截图:

Phone 1: Huawei P20 lite (5.83"):手机 1:华为 P20 lite (5.83"):

https://drive.google.com/file/d/1_wa9bcEa46Ygduo47Q8DNTu9-DIhOv5h/view?usp=drivesdk https://drive.google.com/file/d/1_wa9bcEa46Ygduo47Q8DNTu9-DIhOv5h/view?usp=drivesdk

Phone 2: Samsung Galaxy J1 (4.31"):手机 2:三星 Galaxy J1(4.31 英寸):

https://drive.google.com/file/d/1fNDLea4Vv6LcX-vy-T1S4-XyhHN59pKD/view?usp=drivesdk https://drive.google.com/file/d/1fNDLea4Vv6LcX-vy-T1S4-XyhHN59pKD/view?usp=drivesdk

How can I make them look the same?我怎样才能让它们看起来一样?

You can achieve this by below Options您可以通过以下选项实现此目的

Option 1: Make separate layouts file for each device resolution选项 1:为每个设备分辨率制作单独的布局文件

Option 2: Use Below Library it will automatically support multiple screens.选项 2:使用下面的库它将自动支持多个屏幕。

SDP : https://github.com/intuit/sdp SSP : https://github.com/intuit/ssp SDP: https : //github.com/intuit/sdp SSP: https : //github.com/intuit/ssp

I think you need to spend some more time going through the android documentation specifically focusing on :我认为您需要花更多时间阅读 android 文档,特别关注:

  1. DP vs SP - for font size. DP vs SP - 字体大小。 Understand which one you should use and how font settings (either OS defaults or user overrides) impacts the sizes set for each.了解您应该使用哪种字体以及字体设置(操作系统默认值或用户覆盖)如何影响为每种字体设置的大小。 Go through this SO discussion - What is the difference between "px", "dip", "dp" and "sp"?通过这个 SO 讨论 - “px”、“dip”、“dp”和“sp”之间有什么区别?

  2. Understand the different layouts available on android and when and how to use them.了解 android 上可用的不同布局以及何时以及如何使用它们。 Spend some time understanding unique attributes for each of them.花一些时间了解他们每个人的独特属性。 Have a look at this https://developer.android.com/training/constraint-layout看看这个https://developer.android.com/training/constraint-layout

  3. Understand how padding and margins work and what the difference is.了解填充和边距的工作原理以及它们之间的区别。 If you are using a recyclerview understand how these behave when you apply them to a the recyclerview vs the child.如果您使用的是 recyclerview,请了解当您将它们应用于 recyclerview 与 child 时这些行为的行为。 Trying different combinations and visually seeing the output is the best way to learn.尝试不同的组合并直观地查看输出是最好的学习方式。
  4. Enable “Show Layout Bounds” to help you debug layout issues.启用“显示布局边界”以帮助您调试布局问题。 Look at: https://developer.android.com/studio/debug/dev-options看: https : //developer.android.com/studio/debug/dev-options

You should not need any libraries for what you are trying to achieve.您不应该需要任何库来实现您的目标。 The layout seems very straight forward.布局看起来非常简单。

Add below into your project level Gradle file:将以下添加到您的项目级别 Gradle 文件中:

compile 'com.intuit.sdp:sdp-android:1.0.5'

Usage : In code wherever you are specifying dimensions like 10dp change it to @dimen/_10sdp .用法:在代码中,无论您在何处指定10dp 等尺寸,将其更改为@dimen/_10sdp Like I have specified in this example ImageView.就像我在此示例 ImageView 中指定的那样。

        <ImageView
            android:id="@+id/your_image"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginBottom="@dimen/_10sdp"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:src="@drawable/logo"/>

This will make your application look same on different devices.这将使您的应用程序在不同设备上看起来相同。

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

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