【问题标题】:Overriding textColorPrimary,textColorSecondary for a fragment or ViewGroup or Widgets - AppCompact覆盖片段或 ViewGroup 或小部件的 textColorPrimary、textColorSecondary - AppCompact
【发布时间】:2015-12-02 16:25:49
【问题描述】:

在 AppCompact 中,我们将 colorPrimary、colorPrimaryDark、colorAccent、textColorPrimary 等属性用于应用主题。

我想知道我们能否在片段或视图组中覆盖或将这些颜色更改为不同的颜色。比如,我们可以只为 NavigationView 更改这些颜色,还是只为 Fragment 中的 ExpandableListView 更改这些颜色。

编辑:
我尝试用不同的样式覆盖,但它不是覆盖

<style name="DarkText" parent="AppTheme">
    <item name="android:textColorPrimary">@color/black</item>

    <item name="android:textColorSecondary">@color/black</item>
</style>

<ExpandableListView
    android:layout_height="match_parent"
    android:theme="@style/DarkText"
    android:groupIndicator="@android:color/transparent"
    android:layout_width="match_parent"/>

适配器中的我的 ChildVeiw:

final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);

请注意:我不想在我的 R.layout.list_item 中使用 txtListChild.setTextColor 或 android:textColor="@color/black" 更改文本颜色

【问题讨论】:

    标签: android android-fragments android-appcompat android-theme


    【解决方案1】:

    是的,你可以这样做

    <ExpandableListView
        android:theme="@style/AnotherTheme"/>
    

    编辑:

    将inflate的行改为这个,如果需要替换parent

    convertView = infalInflater.inflate(R.layout.list_item, parent, false);
    

    【讨论】:

    • 这不是覆盖以前的主题,这是我在这里发布的全部原因
    • 另外,这可能取决于您是否正确地为 ListView 的子项充气
    • 请仔细看android:textColorPrimary -> textColorPrimary,试试去掉android:
    • 我认为我们不能在 AppCompat 主题中为 textColorPrimary 删除 android:。它无法找到该属性。 No resource found that matches the given name: attr 'textColorPrimary'.你能告诉我android:textColorPrimarytextColorPrimary之间的区别
    • 你能添加代码来为 ExpandableListView 充气吗?
    【解决方案2】:

    尝试在数组适配器中覆盖 getview()

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), 
            android.R.layout.simple_list_item_1, mStringList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTextColor(Color.BLACK);
            return view;
        }
    };
    

    【讨论】:

    • 动态更改 ViewGroup 的颜色不是问题,我在问我们能否完全覆盖它。我不想在任何地方使用代码更改它。
    猜你喜欢
    • 2019-04-16
    • 2019-05-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多