Android Studio:如何将样式从layout.xml快速移到style.xml中?

30

代码块1: 我在view.xml中有以下代码

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

代码块2:现在我想把它转换成这样的样式。

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

代码块 3:在我的styles.xml文件中,我希望有这个。

<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

将所有这些属性都包裹在<item name="XXX"/>标签中是一个非常手动且令人讨厌的过程。 Android Studio 是否有某种魔法可以自动将代码块1中的属性转换为代码块3中的项标记?

1个回答

56
在编辑.xml文件中的TextView时,按下Ctrl+Shift+A并键入extract。您需要的项目是Style...

15
Android Studio 2+:重构 > 提取 > 样式... - dieter
非常感谢。太棒了!我有很多需要重构的代码,正好及时找到了这个。 - JaydeepW
1
尝试选择布局项而不是属性。 - eljoe
适用于Android:不是应用程序:属性,这并不理想。 - Janusz Hain

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