2014-06-23 13 views

risposta

52
Rect bounds = new Rect(); 
Paint textPaint = textView.getPaint(); 
textPaint.getTextBounds(text,0,text.length(),bounds); 
int height = bounds.height(); 
int width = bounds.width(); 

o

textView.setText("bla"); 
textView.measure(0, 0); 
textView.getMeasuredWidth(); 
textView.getMeasuredHeight(); 
+0

funzionare grande ... thnx. –

+0

ha funzionato come un fascino –

2

provare in questo modo, spero che questo vi aiuterà a risolvere il tuo problema.

yourTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
    int width = yourTextView.getMeasuredWidth(); 
    int height = yourTextView.getMeasuredHeight(); 

    } 
}); 
+0

Ciao @MD, puoi dare un'occhiata a http://stackoverflow.com/questions/25196894/how-to-get-list-item-data-in-bindview-when-clicking-on -radiobutton-in-android –

7

Si prega di provare questo:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView edit = (TextView) findViewById(R.id.edit); 
    edit.setTextSize(20);  
    edit.setText("Hello, world");  
    edit.measure(0, 0); 
    int width = edit.getMeasuredWidth(); 
    Log.w("width", width.toString()); 
} 

Prima di ottenere la larghezza, si deve misurare la vista di modifica/etichetta/testo. Per favore fatemi sapere se questo non funziona.

+0

Entrambe le risposte sono corrette, la tua e quella sopra ... Thnx funziona. –

+0

Plese segna come risposta (il pulsante sotto il basso voto) E sono contento che ha funzionato. – Iosif

2

per favore dimmi larghezza in ??? vuoi ?

TextView metodo getWidth() dà larghezza della vista, espressa in pixel

TextView textView = (TextView)findViewById(R.id.textview); 
textView.getWidth(); //width of your view, in pixels 
+0

Intendevo trovare la larghezza in base al testo. –

0
TextView txt = new TextView(mContext); 
txt.setText("Some Text)"; 
int height = txt.getLineCount() * txt.getLineHeight(); 
int width = txt.getWidth(); 
Problemi correlati