Android Studio中能否为特定后缀XML文件配置自定义代码风格?
Solution for Custom XML Formatting for .layout Files
Absolutely! You can totally configure your IDE to treat .layout files as XML while applying a custom formatting rule that skips attribute reordering (unlike regular XML files). Here's how to do it in JetBrains IDEs (IntelliJ IDEA, Android Studio, etc.—since your Ctrl+Alt+L shortcut points to this ecosystem):
Associate .layout files with XML type (if not done already)
- Open Settings/Preferences (press
Ctrl+Alt+Son Windows/Linux,Cmd+,on Mac) - Navigate to
Editor > File Types - In the top list, select
XML, then click the+button underRegistered Patterns - Type
*.layoutand hit OK to save this association.
- Open Settings/Preferences (press
Create a custom XML code style profile
- Stay in Settings/Preferences, go to
Editor > Code Style > XML - Click the
Managebutton in the top-right corner, then selectCopy - Name the new profile something clear like
XML (No Attribute Reorder) - Switch to this new profile, then go to the
Arrangementtab - Uncheck the
Rearrange attributesoption (this is the key setting to prevent attribute reordering) - Adjust any other formatting settings if needed, then save your changes.
- Stay in Settings/Preferences, go to
Link the .layout pattern to your custom style
- Go back to
Editor > File Typesand find the*.layoutpattern under the XML category - Right-click on
*.layoutand selectAssociate with Code Style - Choose your newly created
XML (No Attribute Reorder)profile from the list and confirm.
- Go back to
Test it out
- Open a regular XML file, press
Ctrl+Alt+L—you’ll see attributes get reordered as usual - Open a
.layoutfile, press the same shortcut—attributes will stay in their original order, while other XML formatting (like indentation) still works as expected.
- Open a regular XML file, press
内容的提问来源于stack exchange,提问作者Cruces
相关产品推荐
相关产品推荐

