简体   繁体   English

Android studio 无法使用 Wrap 内容时,如何为不同屏幕尺寸的多种布局配置应用程序?

[英]Android studio How to configure app for multiple layout for deferent screens sizes when can't use Wrap content?

I hit a brick wall trying to configure my app for multiple screens.我试图为多个屏幕配置我的应用程序时碰壁了。 I read a lot of information about it but so far I didn't manage to successfully do it.我阅读了很多关于它的信息,但到目前为止我还没有成功地做到这一点。 to begin with, I have to use hard-coded sizes for my buttons because there are many of them, 53 to be exact, and they have to be square, 40x40dp, and placed precisely on the layout.首先,我必须为我的按钮使用硬编码的尺寸,因为它们有很多,确切地说是 53 个,它们必须是方形的,40x40dp,并且精确地放置在布局上。 so I can't use wrap_content, fill_parent, etc' because then I can't fit all the buttons on the layout.所以我不能使用 wrap_content、fill_parent 等,因为这样我就不能适应布局上的所有按钮。

I've created 5 different layout 'activity_main.xml' with these screen configuration: normal_mdpi, large-hdpi, xlarge-xhdpi, xxhdpi, xxxhdpi.我用这些屏幕配置创建了 5 个不同的布局“activity_main.xml”:normal_mdpi、large-hdpi、xlarge-xhdpi、xxhdpi、xxxhdpi。 so far so good but if I don't apply (With the "device for preview" dropdown menu) a specific phone configuration to any of the layouts, then they won't fit on the screen.到目前为止一切顺利,但如果我不将特定手机配置(使用“预览设备”下拉菜单)应用于任何布局,那么它们将不适合屏幕。 But then the layouts fit only for these phone configurations, they won't fit for one with a similar configuration but slightly different.但是布局只适合这些手机配置,它们不适合配置相似但略有不同的手机。 what am I doing wrong?我究竟做错了什么? how many layouts do I need?我需要多少布局? do I need to create a layout for each phone out there?我需要为每部手机创建一个布局吗?

You cannot use fixed sizes.您不能使用固定尺寸。 I know you want to use fixed sizes, but you can't.我知道你想使用固定尺寸,但你不能。 Creating multiple layouts for each density is a common mistake.为每个密度创建多个布局是一个常见错误。 There is usually only one xml layout resource file for each activity, but there are exceptions when making really advanced layouts (fragments will be used).每个活动通常只有一个 xml 布局资源文件,但是在制作真正高级的布局时会有例外(将使用片段)。

How to fix the layouts如何修复布局

As the base layout inside the layout resource file, i suggest you use ConstraintLayout as it has a ton of potential and can easily make complex layouts.作为布局资源文件中的基本布局,我建议您使用 ConstraintLayout,因为它具有很大的潜力并且可以轻松制作复杂的布局。 It will take some time to edit each view to make them use wrap_content or match_constraint ( match_constraint is basically match_parent but for ConstraintLayouts), but it is necessary.编辑每个视图以使它们使用wrap_contentmatch_constraint需要一些时间( match_constraint基本上是match_parent但对于 ConstraintLayouts ),但这是必要的。 Android will then take care of resizing and fitting the views for any density/screen size if you use wrap_content and match_constraint ( match_parent for other base layouts like LinearLayout). Android 如果您使用wrap_contentmatch_constraint (其他基本布局如 LinearLayout 的match_parent ),则将负责调整视图大小并适应任何密度/屏幕尺寸。

And remember, never use fixed sizes for views.请记住,永远不要对视图使用固定大小。

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

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