【问题标题】:Android; define theme for specific View subclass within Activity安卓;为 Activity 中的特定 View 子类定义主题
【发布时间】:2012-08-28 23:49:21
【问题描述】:

有没有一种方法可以定义影响给定类型 Activity 中所有视图的样式,而无需编辑所有 xml 视图标签来定义样式。有没有办法定义类似“此 Activity 中的所有 TextViews 的 minHeight 为 70sp”之类的东西?

【问题讨论】:

  • 您为其设置所需视图属性的自定义主题(仅适用于该特定活动)?
  • 嗯,是的,我想。但我只想为特定类型的视图设置样式/主题。我试图避免为每个 TextView 实例显式定义样式,因为似乎应该有一种方法可以做到这一点。如果没有,应该有。

标签: android android-layout android-theme


【解决方案1】:

主题就是你想要的。每个 android 小部件都有一个默认样式,您可以覆盖它以使其成为您想要的。例如:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="TextViewSpecial" parent="@android:style/Theme">
        <item name="android:textViewStyle">@style/SpecialTextView</item>
    </style>
</resources>

覆盖TextView 样式的主题。 SpecialTextView 是这样的风格:

<style name="SpecialTextView" parent="@android:style/Widget.TextView">
    <item name="android:minHeight">70sp</item> <!-- use dp instead of sp --       
</style>

然后只需将清单中的TextViewSpecial 主题设置为所需的活动。您可以使用此方法来覆盖您想要的小部件的样式。

【讨论】:

    【解决方案2】:

    只需为布局定义一个主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多