Applying style software

I did not find a way to do this programmatically, so I post this question here (also I did not find any question related to this).

I have a resource style defined in res / values ​​/styles.xml. What I'm trying to do is apply this style in my activity using java for the View object that I am manipulating.

Is it possible to achieve this in Android, or can a style be applied only to an object using the android: style attribute?

+6
source share
4 answers

No, it’s not possible to broadly apply style resources to an existing instance of a View. Style resources can only be applied to views during construction.

To understand why, look at the View constructor (context context, AttributeSet attrs, int defStyle) . This is the only place where Central View attributes are read (e.g. android: background), so there is no way to apply a style after creating the View. The same template is used for a subclass of a class, such as TextView. You will need to apply style attributes manually using seters.

Note that if you progamatically create an instance of a view, you can use any style resource through the defStyle constructor.

+3
source

I shared this answer here , but since it has its own conversational flow, I feel that it also matters here.

There is no solution to this problem, but it worked for my use. The problem is that the constructor "View (context, attrs, defStyle)" does not refer to the actual style, it wants to get an attribute. So we will:

  • Define Attribute
  • Create the style you want to use
  • Apply style for this attribute in our topic
  • Create new instances of our view with this attribute.

In 'res / values ​​/attrs.xml' define a new attribute:

 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="customTextViewStyle" format="reference"/> ... </resources> 

In res / values ​​/styles.xml "I'm going to create a style that I want to use in my custom TextView

 <style name="CustomTextView"> <item name="android:textSize">18sp</item> <item name="android:textColor">@color/white</item> <item name="android:paddingLeft">14dp</item> </style> 

In 'res / values ​​/themes.xml' or 'res / values ​​/styles.xml' change the theme for your application / action and add the following style:

 <resources> <style name="AppBaseTheme" parent="android:Theme.Light"> <item name="@attr/customTextViewStyle">@style/CustomTextView</item> </style> ... </resources> 

Finally, in your custom TextView, you can now use the constructor with the attribute, and it will get your style

 public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context, null, R.attr.customTextView); } } 

It is worth noting that I have repeatedly used customTextView in different variants and different places, but it does not require that the name of the view match the style or attribute or anything else. In addition, this method should work with any custom view, not just TextViews.

+5
source

No, It is Immpossible. The Resources class, which you usually use to access something from the / res / directory, does not support getting styles. http://developer.android.com/reference/android/content/res/Resources.html

- UPDATE -

What I said here was not quite right. You can specify a style in the constructor of View objects, for example: View (context context, attributeSet attrs, int defStyle) , as well as how crnv talks about some implementation of the view

+2
source

At least for a TextView, this is possible using the setTextAppearance(context, resid) . resId style can be found in R.style. .

+2
source

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


All Articles