原文
在Android Studio 的布局管理器中,你能够迅速的通过在可视化编辑器中拖拽控件而不用在xml文件中手动编辑了。这个编辑器能够在不同的尺寸上的设备预览你的布局。你可以自动调整你的布局保证能在不同的设备上适配。这个布局管理器在你使用ConstraintLayout进行布局的时候功能显得尤为强大。ConstraintLayout是一个提供兼容库兼容到 2.3以上的库。
此页将简单介绍布局编辑器的使用。想学习更多的关于ConstraintLayout的知识,请
介绍
当你用Android Studio 打开一个XML的layout文件的时候就可以看到编辑器了。如下图所示
-
调色板:提供一个空间/布局的控件列表,这些东西你可以直接拖拽到编辑器中。
-
组件列表/组件树:展示你的布局的继承布局关系,点击一下就能在编辑器中看到对应控件被选中。
-
工具栏:提供一些能配置布局的展示效果和能够编辑布局的按钮选项等。
-
设计编辑器:把你的设计图按照设计图纸真实展现出来,还能看到设计的蓝图。
-
属性:提供可以修改控件的一些属性。
小贴士:你可以使用快捷键 Control+Shift+Right/Left ,切换文本/拖拽两种编辑模式。
修改预览效果
上图所示的功能现在来一一介绍:
- 设计蓝图:可以切换到你想展示的样子,窗口中会显示真是的预览效果并且还会显示只包含view轮廓的设计蓝图。这两种试图也可以并排显示。 小贴士:你可以按 B 键进行切换。
- 屏幕方向:你可以横向纵向切换你预览设备。
- 设备类型和尺寸:选择设备类型(手机 /pad/ 手表) 屏幕配置(尺寸、像素密度)。你可以从预先定义好的设备类型、或者你自己定义的设备中选择一个。或者从列表中选择一个新的AVD进行重新定义。 小贴士:你可以拖拽右下角进行缩放操作。
- API version:选择一个Android系统版本预览你的布局。
- App 主题:选择一个主题应用到你的预览中。 提示:只支持 layouts的布局属性,所以有个主题显示会出现问题。
- 语言:选择一个string的资源文件夹显示在你的预览中。只会显示你项目中已经适配的语言资源。如果你想编辑的翻译文件,从下拉菜单中 选择Edit Translations 选项。
- Layout Variants(这个会翻译):从下来菜单中选择一个layout文件应用到你的预览中,或者创建一个新的。
创建一个新的布局文件
当你给应用添加一个新的布局,在你的项目的默认路径layout/ directory 下创建这个文件,这个布局文件就可以在不同的设备上使用。一旦你有一个一个默认的layout布局文件之后,你可以创建一个layout文件的子类应用于特殊的设备上。(比如 配置 xlarge 的屏幕)如果你想创建一个layout另外的配置文件,跳转到创建一个layout 的变种。
另一种创建layout文件的方法如下:
创建layout变量
如果你已经有一个layout文件夹了,你想创建另一个同类型的layout文件针对不同的屏幕尺寸/横屏 优化布局文件 ,使用如下步骤:
Convert a layout to ConstraintLayout
For improved layout performance, you should convert older layouts to ConstraintLayout. Android Studio has a built-in converter to help you do this:
Open your existing layout in Android Studio and click the Design tab at the bottom of the editor window.
Add Views to Your Layout
To start building your layout, simply drag views from the Palette pane into the Design editor. As you place a view in the layout, the editor indicates the view’s relationship with the rest of the layout in a manner appropriate to the type of layout in which you place it.
For example, video 1 shows how dragging a TextView into a ConstraintLayout creates constraints below and aligned left to the above TextView (this is with Autoconnect enabled).
Video 1.The Layout Editor can add constraints for your view when you drag it into the editor
When dragging views into a layout other than ConstraintLayout, the Layout Editor responds differently, as appropriate for the layout properties available for that layout.
Any errors detected in your layout are counted in the toolbar. To view them, click Show Warnings and Errors .
The appearance in the Design editor is for preview only. Although editing your layout in the Design editor can get you far with accurate appearances, you should run your app on an emulator or real device to verify the results.
For more information about how Android’s View APIs work to build a layout, see Layout Fundamentals. Or for a guide to using ConstraintLayout, see Build a Responsive UI with ConstraintLayout.
Edit the view properties
Figure 5. The Properties window
Instead of editing your view properties in XML, you can do so from the Properties window (on the right side of the Layout Editor). This window is available only when the Design editor is open, so be sure you’ve selected the Design tab at the bottom of the window.
Select a view in the editor to see and edit common properties for that view. If you need to access more properties for the view, click View all properties .
When the selected view is a child of ConstraintLayout, the Properties window includes a view inspector at the top with several controls, as shown in figure 7. For more information about the property controls for views in a ConstraintLayout, see Build a Responsive UI with Constraint