java - 使 TextView 中的文本适合屏幕

标签 java android android-layout textview

我希望我的 TextView 中的文本适合屏幕。我需要实现这样的东西:

enter image description here

字母“A”需要位于屏幕中央,高度等于设备屏幕的高度。我已经为此创建了一个自定义 TextView,如下所示,但这似乎不起作用。我的意思是我的文字(字母 A)不适合屏幕的高度。我尝试手动调整文本字体大小,但我猜这不是正确的方法。有人可以为此指出更好的解决方案吗?

 package com.example;

 import android.content.Context;
 import android.content.res.TypedArray;

 import android.graphics.Paint;
 import android.graphics.Rect; 
 import android.util.AttributeSet; 
 import android.util.TypedValue;
 import android.widget.TextView;

 public class FontFitTextView extends TextView
  {
    private Paint mTestPaint;
    private float maxFontSize;
    private static final float MAX_FONT_SIZE_DEFAULT_VALUE = 20f;

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

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

public FontFitTextView(Context context, AttributeSet attributeSet, int defStyle)
{
    super(context, attributeSet, defStyle);
    initialise(context, attributeSet);
}

private void initialise(Context context, AttributeSet attributeSet)
{
    if(attributeSet!=null)
    {
        TypedArray styledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.FontFitTextView);
        maxFontSize = styledAttributes.getDimension(R.styleable.FontFitTextView_maxFontSize, MAX_FONT_SIZE_DEFAULT_VALUE);
        styledAttributes.recycle();
    }
    else
    {
        maxFontSize = MAX_FONT_SIZE_DEFAULT_VALUE;
    }

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

private void refitText(String text, int textWidth, int textHeight)
{
    if (textWidth <= 0)
        return;
    int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
    int targetHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom();
    float hi = maxFontSize;
    float lo = 2;
    final float threshold = 1f; // How close we have to be

    mTestPaint.set(this.getPaint());

    Rect bounds = new Rect();

    while ((hi - lo) > threshold)
    {
        float size = (hi + lo) / 2;
        mTestPaint.setTextSize(size);

        mTestPaint.getTextBounds(text, 0, text.length(), bounds);

        if (bounds.width() >= targetWidth || bounds.height() >= targetHeight)
            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, height);
    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(), this.getHeight());
}

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

XML文件

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:res-auto="http://schemas.android.com/apk/res-auto"
  android:id="@+id/home_Layout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical">

    <com.example.FontFitTextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="1"
        android:textSize="80sp"
        res-auto:maxFontSize="55sp" />

</LinearLayout>

</RelativeLayout>

最佳答案

尝试在 onMeasure() 方法的末尾调用 super.onMeasure() 方法并更新宽度和高度(parentWidth,height)。

关于java - 使 TextView 中的文本适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206139/

相关文章:

java - 从数据库更新/查询的正确方法

Java "new String[-1]"通过编译。怎么来的?

android - 如何让Android的RelativeLayout正常工作?

java - 如何使卡片 View 的移动更加流畅?

android - 保存 TouchListView 并在下次应用打开时打开它

android - 如何使用 Android Espresso 测试 TextInputLayout 值(提示、错误等)?

java - If 语句中的空指针异常

java - 路径中的日期误导了请求 URI

java - 非法状态异常 : Unable to add several TextViews in a View

java - 如何将 Properties 对象从一个 Intent 传递到另一个 Intent ?