如何更改 Android Studio 的默认行为

How to change the default behaviour of Android Studio

我们能否将 Android Studio 的默认构建行为从 Constraint 更改为 Relative Layout?

问题: 有时 Clean/Rebuild 我的项目需要更多时间。而且我从不使用约束布局,所以我想将 Android Studio 的构建行为从 Constraint Layout 设置为 Relative Layout

我希望每次打开 AS 时,它都应该打开 Relative。有没有可能实现?

将当前项目的默认布局设置为 Relative

首先打开 .xml 文件并将 constraint 布局更改为 relative 布局。然后转到 Android Studio 的顶部菜单并打开一个项目。然后转到 "Window" 选项卡,然后 select "Store Current Layout as default"。如果默认布局不是您喜欢的,请下载您想要的默认布局并将其设置为默认布局。

将所有项目的默认布局设为 Relative

只需修改 android 工作室资源中存在的模板布局文件

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout

现在编辑此文件 simple.xml.ftl 并根据您的选择更改布局(注意。 为使默认相对只复制以下代码到您的 simple.xml.ftl 文件) ,请注意某些布局需要额外的元素(例如 LinearLayout 需要 android:orientation),保存文件并在 Android Studio 中创建 activity,它应该可以工作。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
<#if hasAppBar && appBarLayoutName??>
    xmlns:app="http://schemas.android.com/apk/res-auto"
</#if>
    android:id="@+id/${simpleLayoutName}"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
<#if hasAppBar && appBarLayoutName??>
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/${appBarLayoutName}"
</#if>
    tools:context="${relativePackage}.${activityClass}">

<#if isNewProject!false>
    <TextView
<#if includeCppSupport!false>
        android:id="@+id/sample_text"
</#if>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</#if>
</RelativeLayout>

非常简单 - 只需 3 个步骤

  1. 转到C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout
  2. 备份simple.xml.ftl
  3. Modify simple.xml.ftl 到下面的代码并保存

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Sample"
    android:textSize="16sp" />
</RelativeLayout>

Windows 10 build:gradle:3.0.1

上进行了尝试和测试
  • 适用于同一项目中的每个新 activity 以及未来的项目。
  • 注意:需要用管理员权限修改simple.xml.ftl文件
  • 为了解决这个问题 you don't need to edit layout > New > Edit File Templates... 你的 layoutResourceFilelayoutResourceFile_vertical 通过删除 ${ROOT_TAG} - 正如我已经试过了,但没用。

请记住,如果您修改 simple.xml,android studio 更新程序将检测到冲突并阻止应用更新。保留您的副本:)