【问题标题】:Set style to all TextView elements in a layout in one place在一处为布局中的所有 TextView 元素设置样式
【发布时间】:2012-04-27 07:36:06
【问题描述】:

有没有一种方法可以将样式应用于布局 xml 中特定类型的所有元素,而无需实际将样式属性添加到所有元素?

我在 GridLayout 中有很多 TextView 元素。如果我可以说所有 TextView 元素都应该具有“CellFont”样式,而实际上不必将该语句放入每个 TextView 元素中,那就太好了。

解决方案如下:

将 GridLayoutCellFont 样式添加到 style.xml,并将该样式作为 android:theme 添加到应用程序清单中的 Activity。

style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="GridLayoutCellFont">
        <item name="android:textSize">22dp</item>
    </style>

</resources>

清单:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:theme="@style/GridLayoutCellFont"
        android:label="@string/app_name"
        android:name=".TestLayoutsActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

【问题讨论】:

  • 你能分享你的一些代码吗?
  • 添加了解决方案 :)

标签: android layout styles


【解决方案1】:

您可以使用样式或设置新主题的两个选项在Styles and Themes 教程中阅读更多内容

【讨论】:

    【解决方案2】:

    设置android样式或theme.in raw可以定义样式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-16
      • 2014-05-26
      • 2015-07-08
      • 1970-01-01
      • 2015-04-17
      • 2014-07-15
      • 2021-04-25
      • 2012-03-14
      相关资源
      最近更新 更多