如何在Android Studio中从默认的ConstraintLayout切换到RelativeLayout

49

我有最新的Android Studio(2.3 beta 3),创建项目时似乎默认使用了ConstraintLayout。如何让Android Studio将RelativeLayout作为新项目的默认布局元素?

enter image description here

17个回答

46

好的,我看到了上面的答案,它也对我有效。但是,我尝试了一下,成功地将我的当前项目转换为相对布局。请按照以下步骤操作:

在activity_main.xml选项卡中,将其更改为文本。在顶部,您会发现以下内容:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

只需将xmlns之前的所有内容更改为RelativeLayout即可。这样做还会更改最底部的一行,您会发现:

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

问题解决了!开心点:P


7
这个回答没有回答问题,问题是询问如何在创建项目时更改默认布局。 - angryITguy
4
这个回答是针对“当前”项目更改布局的,问题是要更改默认布局。如果你很少使用约束布局,那么在创建每个项目时更改布局是浪费时间的,因此你应该将默认文件模板更改为你想要起始代码。 - sh.e.salh
这个答案和问题有什么关联? - Ali Khaki

15

我是为了Android Studio 2.3.1回答这个问题的。将RelativeLayout设置为默认布局的最简单方法之一是进入文本模式,按照以下方式编辑XML文件:

更改此行:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

请将以下代码粘贴到您的布局文件中:

<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

还要确保您的结束标记更改为:

</android.widget.RelativeLayout>

此外(可选),如果该行以灰色显示,请继续删除此行:

xmlns:app="http://schemas.android.com/apk/res-auto"

编辑:

这是一个可选的项目更改,我在参加Udacity的Android开发者课程时发现了这个提示

如果项目中不需要约束布局,则通过删除以下行从build.gradle中删除此依赖项,然后执行gradle sync:

    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'

14

Android Studio 3.0

步骤0:

关闭 Android Studio。

步骤1:

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

步骤2:

备份 simple.xml.ftl 文件。

步骤3:

将 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/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="12dp"
    android:layout_marginTop="21dp"
    android:text="don't forget to click useful if this helps. this is my first post at stackoverflow!"
    android:textSize="20sp"
    />

</RelativeLayout>

这对我有用。这是唯一一个实际更改默认设置的答案;所有其他答案只会更改当前项目,下次启动新的Android Studio项目时又会回到constraintLayout。 - David Cary
Mac用户:请参考https://dev59.com/3GAg5IYBdhLWcg3wfa96,查找Mac上插件文件夹的位置。 - David Cary

10

适用于Android Studio 3.1

如果您想更改默认的Android Studio设置,请按照以下步骤操作:

第一步:

前往安装了Android Studio的文件目录,例如C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout。

然后:

从其他地方复制simple.xml文件以备份。

接着:

打开simple.xml文件并将其代码替换为以下内容。

<?xml version="1.0" encoding="utf-8"
<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}">



</RelativeLayout>

但是如果你只想改变这个项目的布局,只需要进入activity_main.xml文件,找到文本并将上面的代码粘贴到那里就可以了。


3
这是唯一一个说明如何从“默认布局”更改的答案。 - Chad Bingham

7
  1. 右键单击ConstraintLayout。
  2. 选择convertview。
  3. 选择RelativeLayout。

为了更清晰,请参考下面的图片

enter image description here


6
我认为您想更改Android Studio的默认设置。每当创建布局或活动时,您希望默认创建“RelativeLayout”。在这种情况下,请按照以下步骤操作:
  1. 单击您用于创建布局或活动的任何文件夹
  2. 然后选择“新建 >> 编辑文件模板”
  3. 然后转到“其他”选项卡
  4. 选择“LayoutResourceFile.xml”和“LayoutResourceFile_vertical.xml”
  5. 将“${ROOT_TAG}”更改为“RelativeLayout”
  6. 单击“确定”
完成了!

这对我没有用。我按照两个文件中建议的更改了root_tag为relative layout,但是无效! - Faisal
你能提供一下截图吗? - Shahadat Hossain Shaki
不,但这不是正确的方法,因为如果我们这样做,那么如果我们创建新的布局,那么在新布局对话框中,无论我们指定什么根布局,RelativeLayout 都会被放置在那里。因此,变量 ROOT_TAG 的值需要更改。 - Mohit Atray

5

低效解决方案:

创建新布局

  1. 在Android Studio中进入/res/layout 文件夹
  2. 右键点击 -> 新建 -> 布局资源文件
  3. 给它起个名字并在末尾添加 .xml 后缀
  4. 删除根元素字段并输入 'RelativeLayout'

enter image description here


3

只需右击ConstrainLayout,选择“转换视图”,然后选择“RelativeLayout”:


3
最简单的方法是进入您的.xml文件的文本模式,然后替换顶部行:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

然后继续用以下内容替换它:
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

如果您回到设计模式,您就可以看到现在有了一个相对布局。这也会自动更改结束标签,因此不会有任何问题。


1

试一下这个。它帮助我从 ConstraintLayout 转换到 RelativeLayout

Try this... That helped me to change from ConstraintLayout to RelativeLayout


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接