/ / Android TextView - altura para llenar el espacio máximo y elipsis - android, android-layout, android-textview

Android TextView: altura que ocupa el espacio máximo y elipsisize: android, android-layout, android-textview

Estoy tratando de hacer un diseño como este:

|---------------------------| |---------------------------|
| Title of text goes there  | | Title of text goes there  |
|---------------------------| |---------------------------|
| Image goes there, height  | | Image goes there, height  |
| can be different          | | can be different, very    |
|---------------------------| | very, very different      |
| Long, very long text goes | |---------------------------|
| here, and here, and here  | | Long, very long text goes |
| that should fill space    | | here, and here, and here  |
| lef after title and ima.. | | that should fill space ...|
|---------------------------| |---------------------------|

Imagina ahora que tengo 5 LinearLayout como estesituado en la pantalla, 2 superiores y 3 inferiores, todos de manera uniforme. El problema es que mi texto @Long, muy largo, no se elipsa y llena el espacio disponible al mismo tiempo. Cuando configuro android: maxLines, funciona, pero no tengo las maxLines constantes, porque la altura de la imagen y el título cambios

        <!-- item -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:layout_weight=".3">

<!-- item title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:id="@+id/item_3_title"
android:textSize="22sp"
android:textStyle="bold"/>

<!-- item image -->
<RelativeLayout
android:id="@+id/item_3_image_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">

<ImageView
android:id="@+id/item_3_image"
android:layout_width="wrap_content"
android:layout_height="120dip"
android:scaleType="centerCrop"
android:padding="2dip" >
</ImageView>

<ProgressBar
android:id="@+id/item_3_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>

</RelativeLayout>

<!-- item text -->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="@+id/item_3_text"
android:ellipsize="end"
android:textSize="18sp"/>

</LinearLayout>

Respuestas

8 para la respuesta № 1

Ok, finalmente encontré la respuesta aquí

ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int maxLines = (int) textView.getHeight()
/ textView.getLineHeight();
textView.setMaxLines(maxLines);
textView.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});

1 para la respuesta № 2
 <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:padding="10dip"
>

<!-- item title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:id="@+id/item_3_title"
android:textSize="22sp"
android:textStyle="bold"/>

<!-- item image -->
<RelativeLayout
android:id="@+id/item_3_image_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_3_title"
>

<ImageView
android:id="@+id/item_3_image"
android:layout_width="wrap_content"
android:layout_height="120dip"
android:scaleType="centerCrop"
android:padding="2dip" >
</ImageView>

<ProgressBar
android:id="@+id/item_3_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>

</RelativeLayout>

<!-- item text -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"

android:id="@+id/item_3_text"
android:layout_below="@+id/item_3_image_holder"
android:ellipsize="end"
android:textSize="18sp"/>

</RelativeLayout>

necesita ese tipo de estructura, espero que pueda interpretarla.