Android 如何通过 XML 将文本视图的背景颜色设置为全绿光?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11320044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:33  来源:igfitidea点击:

How to set a textview's background color to be holo green light via XML?

android

提问by Gabe

I've got a textview and I'd let to set its background color to be holo green light as mentioned here.

我有一个文本视图,我可以将其背景颜色设置为全绿光,如此处所述

However, I can't figure out how to do this via XML. Is it possible? I currently have:

但是,我不知道如何通过 XML 做到这一点。是否可以?我目前有:

    <TextView
        android:text="2"
        android:textSize="200sp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/textView2"
        android:background="#00FF00"
        android:gravity="center"
        android:textColor="#EEEEEE"
        android:layout_alignParentRight="true" />

However, I'm unable to change android:background to somehow reference holo green light.

但是,我无法更改 android:background 以某种方式引用全息绿灯。

Does anyone have any suggestions? I've tried "@android:color/" but no dice.

有没有人有什么建议?我试过 "@android:color/" 但没有骰子。

回答by Alex W

Via Java:

通过Java:

TextView test = (TextView) view.findViewById(R.id.textView2);
test.setBackgroundResource(context.getResources().getColor(android.R.color.holo_green_light));

Via XML:

通过 XML:

 <TextView
        android:text="2"
        android:textSize="200sp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/textView2"
        android:style="@style/textviewStyle" 
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:textColor="#EEEEEE"
        android:layout_alignParentRight="true" />

Thisis the API page about this topic

是关于这个主题的 API 页面

回答by Bryan Herbst

android:background="@android:color/holo_green_light"seems to work for me. Your target API version does include the Holo theme, right?

android:background="@android:color/holo_green_light"似乎对我有用。您的目标 API 版本确实包含 Holo 主题,对吗?

回答by Mahmoud Hesham

adding a new info as well it may help someone , u can do this via java as well

添加新信息也可能对某人有所帮助,您也可以通过 java 执行此操作

        textView.setBackgroundResource(R.color.the_color_you_defind_in_your_colors_._xml);