3

Is it possible to inherit android styles from a different layout size?

For example, if I define a style named MyLabel in my "values" directory (i.e. /res/values/style.xml), it is possible to inherit from that label in a different values directory? (i.e. /res/values-large-mdpi/style.xml)

What I'm trying to do is set up a bunch of styles which control the layout of my app, but change the font sizes based on layout size and screen density. I'd like to have most of the styles in one file and then just have the font sizes in the other style files.

1 Answer 1

1

You'd have to do that in two separate files. If you have style.xml in values and values-large-mdpi, the system will automatically use the one in largi-mdpi if that applies and dismiss the other one.

If you have another one like /res/values/styles-common.xml with no counterpart in values-large-mdpi, the system will use that one.

So define all your common styles in the common file and reference them from your specialized styles.

Sign up to request clarification or add additional context in comments.

2 Comments

I created /res/values/style-common.xml which contained a style named "MyStyle" contained height, width and text. Then I created /res/values-xlarge/style.xml which overrode the text attribute. When I start an xlarge emulator the Activity crashes because MyStyle doesn't have a height or width attribute, meaning it did not inherit from the style-common.xml.
Did you set parent to the one in common? You need to have two separate styles, one in the common file and one in the specialized file, and set the parent to the one in the common file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.