【问题标题】:how to format XML code alphabetically in android studio?如何在 android studio 中按字母顺序格式化 XML 代码?
【发布时间】:2019-01-29 20:06:44
【问题描述】:

我有这个 xml 代码。我希望它使用 Command + alt + L 按字母顺序组织。

<ImageView
 android:id="@+id/logoImageView"
 android:layout_width="0dp"
 android:layout_height="150dp"
 android:scaleType="fitCenter"
 android:src="@drawable/ic_logo"
 app:layout_constraintTop_toTopOf="@+id/view"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="64dp"
 android:layout_marginStart="32dp" android:layout_marginEnd="32dp" />

例子

<ImageButton
 android:id="@+id/logoImageView"
 android:layout_width="0dp"
 android:layout_height="150dp"
 android:layout_marginStart="32dp"
 android:layout_marginTop="32dp"
 android:layout_marginEnd="32dp"
 android:background="@android:color/transparent"
 android:scaleType="fitCenter"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

【问题讨论】:

  • 你可以用你想要的任何方式来格式化代码,但字母顺序不是 android 的标准。标准是 id(如果存在)始终位于第一位,因此您可以轻松找到它。然后按该顺序排列布局宽度/高度。然后是处理布局的任何其他参数(这里是 layout_constraint 参数,任何填充/边距等)。然后是任何其他选项。基本上android代码更喜欢按类型分组,而不是按字母顺序分组。特别是因为使用命名空间后,您最终会得到按来源排序的字母,这根本没有意义。
  • 那我怎样才能得到我想要的结果呢?

标签: android xml kotlin


【解决方案1】:

如果您使用的是 Windows 或 Linux,请按 CRTL+ALT+S,进入 -> Editor->Code Style,有一个下拉式 Scheme... https://i.stack.imgur.com/T3vxF.png 选择默认 IDE 并应用,回到您的 XML 文件并按 CRTL+ALT+L,这应该可以工作,如果是 Mac,我还不知道。

【讨论】:

  • 非常感谢,我就知道了。原来在新建项目时,defacto IDE在:SCHEME Project,不得不改成Default。
猜你喜欢
  • 2019-03-30
  • 2016-06-05
  • 2014-04-20
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多