How to use the theme defined in res / values ​​/dimens.xml?

Howdy. In my theme.xml definition, I have the following:

<style name="mythemename">
     <item name="d_myvar">100dip</item>
</style>

I would like to be able to reference this in res / values ​​/dimens.xml as follows:

<dimen name="myvar">?d_myvar</dimen>

Alas, this does not work. When I try to use @ dimen / myvar as the height of LinearLayout, the application crashes with the error "You must specify the layout height attribute".

I also tried

  <dimen name="myvar" value="?d_myvar" />

But this does not compile.

How can I define @ dimen / myvar in my xml so that it loads a variable ? d_myvar defined in the topic?

Thank!

+3
source share
3 answers

I saw your help request at the Italian launch stage.

, dimen:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen
        name="dimension_name"
        >dimension</dimen>
</resources>

. , , :

 ?[package:][type:]name

+2

100dip dimens.xml :

<dimen name="myvar">100dp</dimen>

@dimen/myvar, , R.dimen.myvar

, , dimens.xml, . dimens.xml, / xml.

+2

In styles.xml

<resources>

    <attr format="dimension" name="exampleDimension"/>

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="exampleDimension">100dp</item>
        ...
    </style>
</resources>

Then in another xml, to use the new attribute, you will use it as follows

android:padding="?attr/exampleDimension"
+1
source

All Articles