SORU
11 NİSAN 2010, Pazar


Nasıl metin ayarlamak için yazı tipi boyutu textview uygun

Android herhangi bir şekilde kapladığı alanı uygun bir textview içinde metin boyutu ayarlama var mı?

E. g. Bir TableLayout kullanarak ve her satır için çeşitli textviews ekliyorum. Bu textviews metni kaydırmak istemiyorum beri içeriğinin yazı tipi boyutunu düşürür daha bakın.

Herhangi bir fikir?

MeasureText denedim, ama sütun boyutunu bilemediğim için kullanmak zahmetli görünüyor. Bu uygun bir şey için yazı tipi boyutunu değiştirmek istiyorum kodu

TableRow row = new TableRow(this);   
for(int i=0;i<ColumnNames.length; i  ){    

TextView textColumn = new TextView(this);      
textColumn.setText(ColumnNames[i]);
textColumn.setPadding(0, 0, 1, 0);
textColumn.setTextColor(getResources().getColor(R.drawable.text_default));          

row.addView(textColumn, new TableRow.LayoutParams()); 
} 
table.addView(row, new TableLayout.LayoutParams());  

CEVAP
24 EKİM 2011, PAZARTESİ


Aşağıdaki çözüm önerileri burada tüm içeriyor. Aslında Dunni tarafından yayınlanmıştır ne ile başlar. Gjpc gibi ikili bir ara, ama biraz daha okunabilir kullanır. Ayrıca benim de var gregm. hata düzeltmeleri ve hata düzeltme vardır.

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class FontFitTextView extends TextView {

    public FontFitTextView(Context context) {
        super(context);
        initialise();
    }

    public FontFitTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise();
    }

    private void initialise() {
        mTestPaint = new Paint();
        mTestPaint.set(this.getPaint());
        //max size defaults to the initially specified text size unless it is too small
    }

    /* Re size the font so the specified text fits in the text box
     * assuming the text box is the specified width.
     */
    private void refitText(String text, int textWidth) 
    { 
        if (textWidth <= 0)
            return;
        int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
        float hi = 100;
        float lo = 2;
        final float threshold = 0.5f; // How close we have to be

        mTestPaint.set(this.getPaint());

        while((hi - lo) > threshold) {
            float size = (hi lo)/2;
            mTestPaint.setTextSize(size);
            if(mTestPaint.measureText(text) >= targetWidth) 
                hi = size; // too big
            else
                lo = size; // too small
        }
        // Use lo so that we undershoot rather than overshoot
        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int height = getMeasuredHeight();
        refitText(this.getText().toString(), parentWidth);
        this.setMeasuredDimension(parentWidth, height);
    }

    @Override
    protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
        refitText(text.toString(), this.getWidth());
    }

    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
        if (w != oldw) {
            refitText(this.getText().toString(), w);
        }
    }

    //Attributes
    private Paint mTestPaint;
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • alexis gillis

    alexis gilli

    23 HAZİRAN 2011
  • ItZWaffleS420

    ItZWaffleS42

    9 EYLÜL 2011
  • Kayla Caton - Peet

    Kayla Caton

    23 HAZİRAN 2012