简体   繁体   English

如何为手机、小型平板电脑和大型平板电脑存储 3 个布局文件

[英]How to store 3 layout files for phone, small tablets and big tablets

I have an app with about 20 xml layout files.我有一个包含大约 20 个 xml 布局文件的应用程序。 Altough having spent quite some time to make a generic constraint layout for all devices (which was not possible for me), I now want to create an individual layout file for phones, small tablets and big tabelts.虽然花了很多时间为所有设备制作通用约束布局(这对我来说是不可能的),但我现在想为手机、小型平板电脑和大型平板电脑创建一个单独的布局文件。 So I will have have 20 layout files for phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).所以我将有 20 个布局文件,用于手机(5' - 6.8' 显示屏)、小型平板电脑(7' - 9' 显示屏)和大型平板电脑(超过 9' 显示屏)。

My question is, where shall I store them such that Android automatically picks the correct layout depending on the used device?我的问题是,我应该将它们存储在哪里,以便 Android 根据使用的设备自动选择正确的布局? In this question Layout for tablets in Android it is said that I should store the xml layout files in在这个问题中,Layout for tablets in Android据说我应该将 xml 布局文件存储在

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

or要么

res/layout/my_layout.xml            // layout for normal screen size
res/layout-small/my_layout.xml      // layout for small screen size
res/layout-large/my_layout.xml      // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mod

However, I can't distinguish the devices by their pixel density because different devices with the same display size can have different resolutions.但是,我无法通过像素密度来区分设备,因为具有相同显示尺寸的不同设备可能具有不同的分辨率。 Further, I would like to make the 20 xml layout files not for one specific size but for ranges of display sizes (otherwise I'd have to create too many xml layout files).此外,我想制作 20 个 xml 布局文件,而不是针对一种特定尺寸,而是针对显示尺寸范围(否则我将不得不创建太多的 xml 布局文件)。

How can I define folders for storing xml layout files for ranges of display sizes: phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).如何定义用于存储显示尺寸范围的 xml 布局文件的文件夹:手机(5' - 6.8' 显示屏)、小型平板电脑(7' - 9' 显示屏)和大平板电脑(超过 9' 显示屏)。

Reminder : I'll appreciate every furher comment as I still don't know what to do提醒:我会很感激每一个进一步的评论,因为我仍然不知道该怎么做

Use this dependency.使用此依赖项。

implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.intuit.sdp:sdp-android:1.0.5'

Then apply然后申请

android:layout_width="@dimen/_24sdp"
android:layout_height="@dimen/_24sdp"

For your question and anybody with same issue,对于你的问题和任何有同样问题的人,

  • Use Constraint layout to make flexible UIs and use qualifiers.使用约束布局来制作灵活的 UI 并使用限定符。
  • You will have to have multiple layout files to better support all screen sizes and orientations,您将必须有多个布局文件才能更好地支持所有屏幕尺寸和方向,
  • but no need to make those folders yourself - make a default xml layout for a small mobile screen first, then make an xml layout file with same name as the default one but add a qualifier sw-600dp (or as per use case) to it from 'Available qualifiers' list and adjust the layout.但不需要自己制作这些文件夹 - 首先为小型移动屏幕制作一个默认的 xml 布局,然后制作一个与默认文件同名的 xml 布局文件,但向其添加限定符sw-600dp (或根据用例)从“可用限定符”列表中调整布局。 then make one with sw-700dp and adjust it.然后用sw-700dp制作一个并调整它。
  • Android will automatically detect which layout file to use based on the qualifiers provided. Android 将根据提供的限定符自动检测要使用的布局文件。
  • For reference, please see this example from docs.作为参考,请参阅文档中的示例 Quoting from which Android follows an order of precedence when determining which resources to apply . Android follows an order of precedence when determining which resources to apply This Order is the same in which qualifiers are given in 'Available qualifiers' list to choose from.此顺序与“可用限定符”列表中给出的限定符相同,可供选择。

First of all, you wrote down that you've decided to apply changes in the display based on inch sizes, this is not suggested.首先,您写下您已决定根据英寸尺寸应用显示更改,不建议这样做。 As there are many different digital gadgets with varying pixels in each inch of the display screen, which means just having the upper production dimension doesn't result in better resolution quality;由于有许多不同的数字小工具,在显示屏的每一英寸中具有不同的像素,这意味着仅具有更高的生产尺寸并不能带来更好的分辨率质量; Android provided a unit to solve the problem, it is a ratio: dots per inch "dp". Android提供了一个单位来解决这个问题,它是一个比率:dots per inch "dp"。 Manage a time to look at Android's official guidance on how measuring works:抽时间看看 Android 关于测量工作原理的官方指南:

https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes

Here you can find suitable size dividers & definitions of why are they declared.在这里,您可以找到合适的大小分隔符以及声明它们的原因的定义。

To give your App the ability to choose an appropriate layout to display, you must add new Activity layouts or Fragment layouts in directories based on resolution.为了让您的 App 能够选择合适的布局来显示,您必须根据分辨率在目录中添加新的 Activity 布局或 Fragment 布局。 Just create a layout with the same name as the currently existing one, but change its directory to the desired size category eg: layout sw-600dp, layout sw-840dp, etc. Then design the created layout to be responsible for the mentioned display.只需创建一个与当前现有布局同名的布局,但将其目录更改为所需的大小类别,例如:布局 sw-600dp、布局 sw-840dp 等。然后将创建的布局设计为负责上述显示。

This is how your res may look:这是您的资源的外观:

这是您的资源的外观:

I could provide more clear suggestions if you could express essential parts of your code.如果您可以表达代码的重要部分,我可以提供更明确的建议。

At last, if your App is just Handled by Activities you may need to use fragment a tool that can hold & show different layouts.最后,如果您的应用仅由活动处理,您可能需要使用片段工具来保存和显示不同的布局。 Take look at this:看看这个:

https://developer.android.com/guide/fragments/fragmentmanager https://developer.android.com/guide/fragments/fragmentmanager

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

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