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

Zak*_*rdi 24 xml android android-layout android-studio android-styles

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />
Run Code Online (Sandbox Code Playgroud)

代码块2:现在我想把它移到像这样的样式.

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />
Run Code Online (Sandbox Code Playgroud)

代码块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>
Run Code Online (Sandbox Code Playgroud)

<item name="XXX"/>标记中包装所有这些属性是一个非常手动和烦人的过程.Android Studio是否具有某种类型的魔法来自动将代码块1中的属性转换为代码块3中的项目标记?

Ego*_*uba 39

编辑你的TextView(在布局.xml文件中)按Ctrl+ Shift+ A并输入extract.你需要的项目是Style....

  • Android Studio 2+:`Refactor> Extract> Style ...` (12认同)