You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置Android Studio的Ctrl+Alt+L格式化,使无大括号语句同行

Android Studio设置:让无大括号的条件/循环语句与代码同行格式化

按下Ctrl+Alt+L时,Android Studio默认格式化后的代码如下:

if(...)
  Log.w("...", "...");

else if(...)
  Log.w("...", "...");

else
  Log.w("...", "...");

for(...)
  Log.w("...", "...");

while(...)
  Log.w("...", "...");

etc...

期望的格式化效果:

if(...) Log.w("...", "...");

else if(...) Log.w("...", "...");

else Log.w("...", "...");

for(...) Log.w("...", "...");

while(...) Log.w("...", "...");

etc...

设置步骤:

  • 打开设置面板:Windows/Linux使用File > Settings,Mac使用Android Studio > Settings。
  • 进入代码风格配置:依次展开Editor > Code Style > Java(若为Kotlin项目则选择Kotlin)。
  • 切换到Wrapping and Braces标签页。
  • 针对不同语句修改设置:
    • if/else if/else:找到'if' statement、'else if' statement、'else' clause选项,取消勾选Wrap before body,同时将Force braces设为Never(允许无大括号写法)。
    • for循环:找到'for' statement,取消Wrap before body勾选,Force braces设为Never。
    • while循环:找到'while' statement,取消Wrap before body勾选,Force braces设为Never。
  • 点击Apply和OK保存设置,之后按下Ctrl+Alt+L格式化代码即可实现目标效果。

内容的提问来源于stack exchange,提问作者Taha Sami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:15:36