【问题标题】:Set text color of all elements in Activity设置Activity中所有元素的文字颜色
【发布时间】:2018-05-04 13:36:44
【问题描述】:

有没有办法改变活动中每个元素(复选框、单选按钮等)的完整文本颜色?

CheckBox check = (CheckBox)findViewById(R.id.checkBox);
check.setTextColor(Color.rgb(Rot,Gruen,Blau));

这就是我更改复选框颜色的方式,但我想更改所有元素,可以吗?

【问题讨论】:

  • 不重复!我正在尝试更改 .java 文件而不是 xml 文件中的颜色。
  • 然后在.java文件中设置主题?
  • 可能只需要为用户跟踪 SHARED pref 中的当前颜色,并使用一种方法为您的每个活动拥有所有视图 setColorProperty 并在 ViewLoad 之后更改它!我的意思是在设置设计后在 ONCREATE 中使用它
  • 或者可能在设置 contentVirw 后,在 ONCREATE 方法中膨胀整个设计并获取 rootView 组并再次为所有视图设置 textColor 属性

标签: java android


【解决方案1】:

希望它可以帮助您解决问题。

private void setAppFontColor(ViewGroup mContainer, int color) {
    if (mContainer == null || color == 0)
        return;

    final int mCount = mContainer.getChildCount();

    for (int i = 0; i < mCount; ++i) {
        final View mChild = mContainer.getChildAt(i);
        if (mChild instanceof TextView) {
            ((TextView) mChild).setTextColor(color);
        } else if (mChild instanceof Button) {
            ((Button) mChild).setTextColor(color);
        } else if (mChild instanceof ViewGroup) {
            setAppFontColor((ViewGroup) mChild, color);
        }
    }
}

在哪里

ViewGroup mContainer = (ViewGroup) findViewById(android.R.id.content).getRootView();

【讨论】:

  • 完美运行!谢谢!
【解决方案2】:

是的,将textColorSecondary 添加到您的活动主题中

<item name="android:textColorSecondary">yourcolor</item>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2012-10-01
    • 2021-02-05
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多