Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a max width on a LinearLayout so that it doesn't stretch in Landscape mode?

My app looks great in portrait mode, but it stretches out in landscape and looks bad. I know I could make a separate layout for landscape, but I'd rather just set my root element's maximum width so that landscape mode just results in some white space on the sides. Is there a way to do this?

like image 859
NSouth Avatar asked Aug 29 '14 18:08

NSouth


2 Answers

Keeping different layout files is a correct solution but it adds complexity to your project (you'd have to maintain multiple files in case the designer redesigns the view layout).

If all you need is to alter the width of a LinearLayout, you can do the following. Keep one xml file in the layout folder and give it a value of

<LinearLayout
    style="@style/width_match_parent_max_200"
    android:layout_height="wrap_content">

then, in your values-600dp/styles.xml write:

<resources>
<style name="width_match_parent_max_200">
    <item name="android:layout_width">200dp</item>
</style>
</resources>

and in your values/styles.xml:

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

like image 120
kouretinho Avatar answered Nov 10 '22 00:11

kouretinho


The BoundedViews library allows to set boundedWidth/boundedHeight on views even layout_width="match_parent"

like image 21
Bao Le Avatar answered Nov 09 '22 23:11

Bao Le