2

I have a View which width must depend on the screen width:

  • it must fill the screen on small screens (less than 600dip wide)
  • it must have a fixed width (600dip) on bigger screens

I was hoping I could use dimensions stored in XML files:

mylayout.xml:

 <View
   android:layout_height="100dip"
   android:layout_width="@dimen/myview_width"/>

values-sw600dp/dimensions.xml:

    <dimen name="myview_width">600dip</dimen>

values/dimensions.xml:

    <dimen name="myview_width">FILL_PARENT</dimen> <!-- NOT SUPPORTED -->

... but the use of FILL_PARENT is not supported in dimen elements.

What's the cleanest way to achieve this in XML ?

Sébastien
  • 13,831
  • 10
  • 55
  • 70

1 Answers1

11

You should use styles for different screens. Like this:

 <View
   style="@style/MyStyle"
   android:layout_height="100dip"/>

in values-sw600dp/my_style.xml:

    <style name="MyStyle">
        <item name="android:layout_width">600dip</item>
    </style>

in values/my_style.xml:

    <style name="MyStyle">
        <item name="android:layout_width">match_parent</item>
    </style>

With this you can do what you want.

R4ng3LII
  • 457
  • 3
  • 8