【问题标题】:Android Studio: How to quickly move styling from layout.xml to style.xml?Android Studio:如何快速将样式从 layout.xml 移动到 style.xml?
【发布时间】:2015-02-03 15:44:55
【问题描述】:

代码块 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>

将所有这些属性包装在&lt;item name="XXX"/&gt; 标记中是一个非常手动且烦人的过程。 Android Studio 是否有某种类型的魔法可以自动将代码块 1 中的属性转换为代码块 3 中的项目标签?

【问题讨论】:

    标签: android xml android-layout android-studio android-styles


    【解决方案1】:

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

    【讨论】:

    • Android Studio 2+ : Refactor &gt; Extract &gt; Style...
    • 非常感谢。作品!我有很多代码要重构,及时发现。
    • 尝试选择布局项而不是属性
    • 适用于 android: not app: attributes, 这并不理想
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多