I have a layout which is used on both landscape and portrait. I have it in /layout
and /layout-land
folders. The only difference is the android:orientation
.
I would like to have only 1 xml in /layout
because it is easier to maintain. I'd like to use /values
and /values-land
instead. Is this possible?
What I have now is (unnecessary code removed):
<LinearLayout
android:orientation="horizontal"/>
vs
<LinearLayout
android:orientation="vertical"/>
I though about something like:
<LinearLayout
android:orientation="@xxxx/linear_orientation"/>
As an ultimate solution I also thought about creating a custom style only for this LinearLayout
(I don't like this solution, but will have to use it if I can't fine another option).
Note: I know it is possible to do it on java
, but I'm looking for a xml-only solution.
I would like to have only 1 xml in /layout because it is easier to maintain. I'd like to use /values and /values-land instead. Is this possible?
Yes, it's possible. You can use Style to achieve that.
The Style in the styles.xml under /values:
<style name="LinearLayoutOrientation" > <item name="android:orientation">vertical</item> </style>
The Style in the styles.xml under /values-land:
<style name="LinearLayoutOrientation" > <item name="android:orientation">horizontal</item> </style>
Your final xml code for LinearLayout:
<LinearLayout
style="@style/LinearLayoutOrientation"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With