开发者

Applying style resource programmatically

开发者 https://www.devze.com 2023-04-06 04:48 出处:网络
I didn\'t find a way to do that programmatically so I am posting this question here (also i didn\'t find any question related to this one).

I didn't find a way to do that programmatically so I am posting this question here (also i didn't find any question related to this one).

I have a resource style, defined in the res/values/styles.xml. What I am trying to do is apply this style inside my开发者_JAVA百科 activity using java to a View object that i am manipulating.

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


Shared this answer here, but since this has it's own conversational thread, I feel like its relevant here as well.

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

  1. Define an attribute
  2. Create a style that you want to use
  3. Apply a style for that attribute on our theme
  4. Create new instances of our view with that 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 the style I want to use on 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', modify the theme for your application / activity 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 receive your style

public class CustomTextView extends TextView {

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

It's worth noting that I repeatedly used customTextView in different variants and different places, but it is in no way required that the name of the view match the style or the attribute or anything. Also, this technique should work with any custom view, not just TextViews.


No, it isn't possible to generically apply a style resources to an existing View instance. Style resources can only be applied to Views during construction time.

To understand why, study the View(Context context, AttributeSet attrs, int defStyle) constructor. This is the only place where central View attributes (like android:background) are read, so there is no way to apply a style after the View has been constructed. The same pattern is used for sub classes of View, like TextView. You will need to apply the style attributes manually using setters.

Note that if you progamatically instantiate the View, you can use any style resource through the defStyle constructor parameter.


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


No this is not possible. The Resources class you normally use to access anything from the /res/ directory has no support for getting styles. http://developer.android.com/reference/android/content/res/Resources.html

-- UPDATE --

What I said here wasn't completely right. You can give a style in the constructor of View objects like this: View(Context context, AttributeSet attrs, int defStyle) and also like crnv says for some View implementations

0

精彩评论

暂无评论...
验证码 换一张
取 消