2012-05-28 14 views

Respuesta

1
((TextView) ((FrameLayout) ((LinearLayout) ((ViewGroup) getWindow() 
      .getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)) 
      .setEllipsize(TextUtils.TruncateAt.MIDDLE); 

lo tiene desde android:set title bar in middle

+6

Esto es un truco. Es probable que se rompa. – pjv

+0

Estoy de acuerdo, rompe con 3.0+, pero también funciona con personas mayores :) – xmen

-3

Intente utilizar android:ellipsize

Consulte android:ellipsize en la página de desarrolladores de Android.

+0

usted no puede utilizarla en un título de Actividad, ¿puedes? – xmen

21

Usted puede hacer esto:

final int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 
    final TextView title = (TextView)getWindow().findViewById(actionBarTitle); 
    if (title != null) { 
     title.setEllipsize(TextUtils.TruncateAt.MIDDLE); 
    } 
+0

no funciona, no creo que exista actionbar en Android 1.6 – xmen

+1

No veo el 1.6 en su pregunta. Debería ser el mismo, pero una identificación diferente, intente encontrarlo usando hierarchyviewer –

+3

. Esto también es un hack. Es probable que se rompa. Al menos verificar los nulos y detectar excepciones. – pjv

0

si el usuario la barra de herramientas puede añadir esta función antes de setTitle

private void setToolbarTextEllipsizeInMiddle(Toolbar toolbar){ 
    try { 
     toolbar.setTitle("'"); 
     for (int i = 0; i < toolbar.getChildCount(); i++) { 
      View view = toolbar.getChildAt(i); 
      if (view instanceof TextView) { 
       TextView tv = (TextView) view; 
       tv.setEllipsize(TextUtils.TruncateAt.MIDDLE); 
      } 
     }//end for i 
     toolbar.setTitle(""); 
    } catch (Exception e) { 
    } 
} 
Cuestiones relacionadas