Utilizar 'ellipsize' en un TextView y estirar la vista SOLAMENTE si es necesario

Tengo 2 TextViews en una fila y 2 requisitos:

1) Si el primer TextView no es demasiado ancho, debe mirar como sigue

| [1 texto] [2 texto] |

2) Si el primer TextView es demasiado ancho, debería verse como sigue

| [1 texto texto tex …] [2 texto] |

El segundo requisito es simple, puedes usar android: layout_weight = "1", por ejemplo:

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" android:text="1 text text text text text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2 text" /> </LinearLayout> 

, Pero si el primer TextView contiene un texto corto, parece

| [1 texto] [2 texto] |

, Lo que no es aceptable.

Entonces, ¿cómo satisfacer ambos requisitos 1) y 2) al mismo tiempo?

Mientras tanto, encontré una solución muy sencilla: simplemente establezca el ancho de LinearLayout en "wrap_content" en lugar de "fill_parent".

 <LinearLayout android:orientation="horizontal" android:layout_width="WRAP_CONTENT" android:layout_height="wrap_content" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" android:text="1 text text text text text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2 text" /> </LinearLayout> 

No creo que pueda hacer esto usando solo el diseño. Para que Android pueda ellipsizar Text1, tiene que saber el ancho exacto del TextView . Sólo puede hacer esto ya sea dándole un tamaño fijo o dando a las otras vistas tamaños fijos. Usted no quiere hacer ninguno de estos.

Necesita medir el ancho del texto en cada TextView como si fuera a renderizar . Una vez que sepa el ancho que cada texto tomará, puede tomar decisiones en el código acerca de cómo obtener el diseño para hacer lo que desea.

Agregue otra View al LinearLayout que tenga android:layout_weight="1000" . Esto ocupará todo el espacio no utilizado si el ancho de text1 y text2 combinado no excede el ancho de pantalla. Ahora calcule el ancho de text1 y el ancho de text2 así:

 Rect bounds = new Rect(); Paint textPaint = textView1.getPaint(); textPaint.getTextBounds(text1, 0, text1.length(), bounds); int widthText1 = bounds.width(); textPaint = textView2.getPaint(); textPaint.getTextBounds(text2, 0, text2.length(), bounds); int widthText2 = bounds.width(); 

Ahora ya sabes el ancho que text1 y text2 requerirían si estuvieran completamente renderizados.

 if (widthText1 + widthText2 > screenWidth) { View3.setVisibility(View.GONE); // Don't need View3 as there is no extra space } 

En un caso, View3 ocupará todo el espacio restante. En el otro caso, TextView1 debe ser ellipsized al final.

Realmente no he probado esto, así que no seas demasiado duro conmigo si no funciona.

  • Layout: ¿cómo hacer que la imagen cambie su anchura y altura proporcionalmente?
  • El texto personalizado de TextView no muestra texto pero sólo muestra el fondo
  • ¿Por qué LayoutInflater ignora los parámetros layout_width y layout_height que he especificado?
  • Principiante Android Development: findViewById issue
  • findViewByID () sigue devolviendo null en la clase de vista personalizada
  • Android: RecyclerView dentro de un ScrollView (o paralaje)
  • Visibilidad de vista de imagen en Android
  • ¿Cómo quitar la pantalla blanca mientras se carga la aplicación de Android?
  • Linear Layout - Diferencia entre el peso y FILL_PARENT
  • Android - Compuesto personalizado generado dinámicamente Ver recreado en RecyclerView, causando un rendimiento bajo
  • No se puede encontrar la clase RecyclerView android.support.v7.recyclerview.R $ styleable
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.