Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a max width for LinearLayout

How can I set the max width of my horizontal LinearLayout? So if the contents are short (say, some text), the layout shrinks and if the contents are longer, it will not expand more than some max width value.

I prefer doing this at the XML level.

like image 732
Asahi Avatar asked Sep 13 '10 15:09

Asahi


People also ask

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

What is difference between LinearLayout and RelativeLayout?

Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.

How do you declare a LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .


1 Answers

This does it - what I needed beyond what was suggested in prior answer is to add another layout (@+id/TheLayout) as a wrapper between the content and the top layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:paddingLeft="100dip"
android:paddingRight="100dip">
<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/TheLayout"
    android:layout_gravity="right">
    <TextView android:id="@+id/TextView01" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:layout_weight="1"
        android:text="this is my text." 
        android:layout_gravity="center_vertical">
    </TextView>
    <ImageView android:id="@+id/ImageView01"
        android:layout_height="wrap_content" 
        android:background="@drawable/icon"
        android:layout_width="fill_parent">
    </ImageView>
</LinearLayout>
</LinearLayout>
like image 175
Asahi Avatar answered Sep 23 '22 10:09

Asahi