如何自定义Android Studio模板?替换默认Constraint Layout为Relative Layout
Hey there! I totally get your frustration—Constraint Layout is powerful, but when most of your work relies on Relative Layout, having Android Studio default to the former every time you create a new project gets old fast. Let me share two solid ways to fix this by customizing the templates, tailored for Android Studio 3.0.
Method 1: Modify the Built-in Activity Template (Quick Fix)
Android Studio stores all its default templates in its installation directory. Here's how to tweak the blank activity template to use Relative Layout:
Locate the template folder
- On Windows: Navigate to
C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\BlankActivity\root\res\layout - On Mac: Go to
/Applications/Android Studio.app/Contents/plugins/android/lib/templates/activities/BlankActivity/root/res/layout - Note: If you use other activity templates (like Basic Activity), head to the corresponding folder (e.g.,
BasicActivity) instead ofBlankActivity.
- On Windows: Navigate to
Edit the layout template file
- Open
activity_blank.xml.ftl(or the matching layout file for your target activity template) with a text editor. - Replace every instance of
ConstraintLayoutwithRelativeLayout, and delete allapp:layout_constraint*attributes since they're specific to Constraint Layout. - A simplified example of the modified file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerInParent="true"/> </RelativeLayout> - Save the file, then restart Android Studio. Your new projects will now default to Relative Layout when you enable the "Generate Layout" option.
- Open
Method 2: Create a Custom Activity Template (Safe & Update-Proof)
Modifying built-in templates can be risky—Android Studio updates might overwrite your changes. A better long-term solution is to make your own custom template:
Set up the custom template directory
- Find your Android Studio 3.0 config folder:
- Windows:
C:\Users\[Your Username]\.AndroidStudio3.0\config\templates - Mac:
~/Library/Application Support/AndroidStudio3.0/templates
- Windows:
- If the
templatesfolder doesn't exist, create it. Then add a subfolder structure:activities/RelativeLayoutBlankActivity.
- Find your Android Studio 3.0 config folder:
Copy the built-in template content
- Copy all files from the built-in
BlankActivityfolder (the same one from Method 1) into your newRelativeLayoutBlankActivitydirectory.
- Copy all files from the built-in
Customize the template
- Open
template.xmland change thenameattribute to something like"Blank Activity (Relative Layout)"—this is what will show up in the activity creation menu. - Edit the layout file in
root/res/layoutexactly like you did in Method 1, swapping Constraint Layout for Relative Layout.
- Open
Use your new template
- Restart Android Studio. When creating a new project or adding a new activity, you'll see your custom template option. Select it, and you'll get a fresh layout based on Relative Layout every time.
Quick Bonus Tip
If you only need Relative Layout occasionally, you can manually replace Constraint Layout after creating the project—but template customization is the way to go for a one-and-done fix that matches your workflow.
内容的提问来源于stack exchange,提问作者Alex

