2010-09-13 21 views
7

¿Cómo puedo establecer el ancho máximo de mi horizontal LinearLayout? Por lo tanto, si los contenidos son cortos (por ejemplo, algunos textos), el diseño se contrae y si el contenido es más largo, no se expandirá más que un valor de ancho máximo.Configuración de un ancho máximo para LinearLayout

Prefiero hacer esto en el nivel XML.

+0

ver mi respuesta [aquí] (http://stackoverflow.com/a/8619442/236743) para otro enfoque utilizando una subclase de ViewGroup – Dori

Respuesta

7

Esto lo hace - lo que necesitaba beyond what was suggested in prior answer es añadir otro diseño (@+id/TheLayout) como un envoltorio entre el contenido y el diseño de la parte superior:

<?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> 
+0

¡Agradable! Trabajando como un encanto. –

0
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST); 
    super.onMeasure(widthMaxSpec, heightMeasureSpec); 
} 
+0

bienvenido a SO, usted prefiere explicar por qué su código resolverá su problema, gracias. –

Cuestiones relacionadas