Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include tools namespace in a style xml

I want to be able to use

tools:text="Text"

in a style.xml file, this way don't duplicate tools:text in all my layout that are using the style.

I've already tried:

<item name="text">@string/text</item>

but when is present the editor doesn't show the text.

Thanks for your help!

like image 923
Arturo Mejia Avatar asked Feb 18 '18 12:02

Arturo Mejia


People also ask

Where are styles created in XML?

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

What is Tools layout_editor_absoluteX?

Layout editor AbsoluteXtools:layout_editor_absoluteX="60dp" The above takes the screen and give it a margin left(horizontal) of 60dp from the start of screen. Thats means even if it isn't constrained, it won't jump to top-left of screen at runtime.

What is styles XML?

Note: A style is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine style resources with other simple resources in the one XML file, under one <resources> element. The filename is arbitrary.


1 Answers

According to the documentation: https://developer.android.com/studio/write/tool-attributes.html#design-time_view_attributes

tools: instead of android:

Intended for: <View>

Used by: Android Studio layout editor

Unfortunately, this means you can't use tools:text inside a <style> element.

like image 178
Ben P. Avatar answered Sep 21 '22 05:09

Ben P.