Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set LinearLayout Orientation using references instead of layout-land (only xml)

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.

like image 317
gian1200 Avatar asked Sep 28 '14 03:09

gian1200


1 Answers

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"/>
like image 148
Lei Guo Avatar answered Sep 28 '22 02:09

Lei Guo