Android sets background color in TextView in styles.xml

In my application, I want to set a style in a TextView that will make the TextView look like a title, as shown in the following figure -

"General" is the heading in this image.

When I apply the style from styles.xml (given below), it applies the font and font color as indicated. But on a white background, it does not apply. Can we even do this?

A specific style is as follows:

 <?xml version="1.0" encoding="utf-8"?> 

 <style name="settings_header"> <item name="android:layout_marginBottom"> 10dip </item> <item name="android:background"> @color/white </item> <item name="android:paddingLeft"> 10dip </item> <item name="android:layout_width"> match_parent </item> <item name="android:layout_height"> wrap_content </item> <item name="android:textSize"> 22sp </item> <item name="android:textColor"> @color/black </item> <item name="android:textStyle"> bold </item> </style> 

The code that applies the style is similar to

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textview = (TextView) findViewById(R.id.textView); textview.setTextAppearance(ScrSettings.this,R.style.settings_header); } 
+4
source share
4 answers

Finally, I found a way to do this.

 textview.setTextAppearance(context, R.style.settings_header); textview.setBackgroundResource(R.color.white); 

The question is still not fully answered, since setting the background in the TextView twice is not what we want. We already set the background in styles.xml .

+1
source

I'm not right now if his help

try this @ Color / # FFF

instead of "white"

luck

0
source

if you do not have a color folder in your permissions, you need to replace @ color / white with #ffffff

0
source

public void setTextAppearance (context context, int remainder)

Sets the color of the text color , size , style , color, and highlight color from the specified TextAppearance resource.

This is the actual output of setTextAppearance . Why do you think about background color without reading the behavior of the setTextAppearance method ? .

If you want all the properties that you set in styles.xml , use style="@style/settings_header" only in the XML file.

I hope you understand this.

EDIT

To set the background color at run time, use the setBackgroundColor method.

0
source

Source: https://habr.com/ru/post/1394988/


All Articles