【问题标题】:Generate style from xml in intellij (/android studio)在 intellij (/android studio) 中从 xml 生成样式
【发布时间】:2016-10-22 23:39:53
【问题描述】:

有没有办法从 XML 属性生成样式,这样我就不必手动一一复制粘贴?当我制作原型时,我使用“内联”样式,然后我想将样式导出到 styles.xml 但手动这是一项耗时的任务。只是格式不同,应该很容易自动化。

<ImageView
        android:layout_width="80dp"
        android:layout_height="47dp"/>

进入

<ImageView
        style="@style/h2_image"/>

<style name="h2_image">
    <item name="android:layout_width">80dp</item>
    <item name="android:layout_height">47dp</item>
</style>

【问题讨论】:

    标签: android xml android-studio intellij-idea


    【解决方案1】:

    非常简单!

    只需将光标保持在要从中提取样式资源的文件中即可。只需右键单击,然后单击重构。

    重构 -> 提取 -> 样式/布局

    然后给样式命名,然后就可以了..! 更多内容可以参考documentation

    【讨论】:

    • 哦,很酷,我想一定有一些简单的方法可以做到这一点
    • 谢谢你,这让我的生活改善了 1000%。
    【解决方案2】:

    试试这样的:

    <resources>
        <style name="CustomImgView">
            <item name="android:layout_width">80dp</item>
            <item name="android:layout_height">47dp</item>
        </style>
    </resources>
    
    <ImageView style="@style/CustomImgView" />
    

    更多信息:https://developer.android.com/guide/topics/resources/style-resource.html

    【讨论】:

    • 哦 - 我不够清楚 - 请查看更新的答案(我知道如何使用样式)
    【解决方案3】:

    尝试像这样声明你的风格:

    <?xml version="1.0" encoding="utf-8" ?>
          <resources xmlns:android="http://schemas.android.com/apk/res/android">
            <style name="YourCustomStyle">
                <item name="android:layout_width">"80dp"</item>
                <item name="android:layout_height">"47dp"</item>
                 <!-- another attributes -->
                <item name="customAttr">value</item> 
            </style>
          </resources>
    

    在你的xml 中添加这个android:theme="@style/YourCustomStyle"

    【讨论】:

    • 哦 - 我不够清楚 - 请查看更新的答案(我知道如何使用样式)
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2018-08-08
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多