How to inherit fields in android xml?

I want to set some properties for the first element ImageView

and then inherit the properties in other elements.

The following don't work, but is there probably something similar?

<ImageView
            android:id="@+id/myimage"
            android:layout_height="100dp"
            android:layout_width="100dp"/>

<ImageView
            android:id="@+id/myimage2"
            android:layout_height="@myimage/layout_height"
            android:layout_width="@myimage/layout_width"/>

      

+3


source to share


1 answer


You cannot do it this way, but you have 2 options:

Option 1 : declare the value as a dimension and use it:

Declare two values ​​for height and width in dimens.xml

, which can be found in res/values

.

<dimen name="image_width">100dp</dimen>
<dimen name="image_height">100dp</dimen>

      

Then use them in your xml like this:

<ImageView
        android:id="@+id/myimage2"
        android:layout_height="@dimen/image_height"
        android:layout_width="@dimen/image_width"/>

      



Option 2 . Create style

linked to yours ImageView

:

The first thing to do is create a style for your views and place it in styles.xml

, which can be found in res/values

.

<style name="my_image_view_style">
    <item name="android:layout_height">100dp</item>
    <item name="android:layout_width">100dp</item>
</style>

      

Add created style

to your ImageView

:

<ImageView
        android:id="@+id/myimage2"
        style="@style/my_image_view_style"/>

      

+2


source







All Articles