如何在包含多个TextView的LinearLayout中设置默认文本颜色?

3

我有一个LinearLayout,其中有多个TextView,并希望仅为该布局设置默认全局颜色,而无需在每个TextView中添加textColor字段。此外,如果可能的话,是否也可以通过在TextView中添加它来覆盖颜色值?例如,如果我将蓝色设置为默认颜色,并将黑色设置为单个TextView的颜色,那么蓝色会变成黑色吗?


1
创建一个样式并将该样式设置为您拥有的每个TextView:) - hardartcore
@hardartcore,你的方法和设置setTextColor()之间没有区别。OP试图避免单独视图分配。 - Taslim Oseni
@Taslim 这真的取决于具体情况! :) 如果没有解释他想用这些 TextView 实现什么,我不认为有最好和正确的答案! - hardartcore
@hardartcore,你说得对。我可能下结论太仓促了。真的无法确定OP想要实现什么。抱歉误解了。 - Taslim Oseni
5个回答

2
你可以通过在styles.xml中为父元素设置textColorPrimary和textColorSecondary来覆盖整个应用程序的默认文本颜色。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="textColorPrimary">@color/black</item>
    <item name="textColorSecondary">@color/grey</item>
</style>


2

为了设置默认的全局TextView颜色,您首先可以在AndroidManifest.xml文件中为以下项目创建自己的主题:

  • textColorPrimary - 用于大文本
  • textColorSecondary - 用于中等文本
  • textColorTertiary - 用于小型文本
  • textColorHint - 用于提示文本

例如,在AndroidManifest.xml中:

<style name="TextViewTheme" parent="android:Widget.TextView">
    <!-- Set the default global color for TextViews to Holo Blue Dark -->
    <item name="android:textColorPrimary">@android:color/holo_blue_dark</item>
    <item name="android:textColorSecondary">@android:color/holo_blue_dark</item>
    <item name="android:textColorTertiary">@android:color/holo_blue_dark</item>
    <item name="android:textColorHint">@android:color/holo_blue_dark</item>
</style>

接下来,在您的LinearLayout上设置主题样式。您还可以为单个TextView覆盖默认值,例如在activity_main.xml中将第一个TextView的Hint文本颜色设置为黑色,如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:theme="@style/TextViewTheme">

    <TextView
        android:id="@+id/phone_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/phone_tv"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/black" />

     <TextView
        android:id="@+id/email_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/email_tv" />

</LinearLayout>

希望这能帮到您!

1

即使您在布局中添加或删除TextViews,此代码也能正常工作。只需将其放入活动的onCreate()中即可;

    LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        if (v instanceof TextView) {
            ((TextView) v).setTextColor(Color.BLACK);
        } 
    }

将颜色更改为您喜欢的颜色。
如果您想在此代码之后更改任何特定TextView的颜色,可以这样做。


1
很晚回答,但这可以解决问题。 在 style.xml 中最近更改为 themes.xml
<style name="ErrorStyle">
        <item name="android:textColor">#FF0000</item>
</style>

然后在你的xml布局文件中:
<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:theme="@style/ErrorStyle">

 .. All of your TextView
</LinearLayout>

使用这种方法,我们可以使用其他XML变体,例如夜间模式等,并仍然有覆盖内部TextView的可能性。

1
如果你的 TextView 数量非常多,以至于在每个 TextView 上调用 setTextcolor() 方法都会是一个巨大的任务,为什么不使用支持适配器(如 ListView、RecyclerView 等)的 View 呢?使用适配器后,你可以设置一个模型 TextView 布局,并为所有 TextView 设置全局 textColor。你可以使用简单的 if 和 else 语句来覆盖适配器中的全局 textColor。
希望这能帮到你,祝编码愉快!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接