最近Google更新了AS为2.3版本,由于现在大力推行ConstraintLayout布局,所以在新建工程的时候默认的布局也会变为ConstraintLayout,其实ConstraintLayout用起来还是蛮方便的,详见郭霖老师的文章
但是,如果我们仅仅想写一个Demo用ConstraintLayout难免有点大材小用,操作起来也不如直接配置xml文件那么简单。那么Android Studio能否修改新建工程的默认布局呢?非常遗憾在setting里面暂时是无法实现的,但是我们可以通过修改默认布局的文件来达到我们的目的。
以下以将ConstraintLayout修改为LinearLayout为例,介绍以下修改方法,其他的举一反三即可。
首先打开你的Android Sudio安装目录,我的为D:\Program Files\Android\Android Studio,进入到以下文件夹\plugins\android\lib\templates\activities\common\root\res\layout,
然后使用文本编辑器打开simple.xml.ftl文件:如下
?xml version="1.0" encoding="utf-8"?app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/${appBarLayoutName}"#iftools:context="${packageName}.${activityClass}"><#if isNewProject!false>android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
是不是很熟悉?没错这就是ConstraintLayout的布局文件,只不过有一些条件判定,我们无视它,直接改成LinearLayout的布局代码
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
然后保存即可。注意:为了防止错误操作以及方便日后还原,最好在修改前备份一下simple.xml.ftl文件。
现在,再重新打开AS,新建一个工程,默认的布局就变成了LinearLayout了,是不是很简单呢?