membersound membersound - 3 years ago 165
Android Question

How to inherit fields in android xml?

I want to set some properties for the first

ImageView
element, and then inherit the properties in other elements.

The following does not work, but probably there is 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"/>

Answer Source

You can't do that in that way, but you have 2 options here:

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

Declare two values for height and width in dimens.xml that can be found at 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 a style associated with your ImageView:

First thing to do is to create the style for your views and place it in styles.xml that can be found at res/values.

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

Add your created style to your ImageView:

<ImageView
        android:id="@+id/myimage2"
        style="@style/my_image_view_style"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download