Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使文本视图中的文本适合屏幕_Java_Android_Android Layout_Textview - Fatal编程技术网

Java 使文本视图中的文本适合屏幕

Java 使文本视图中的文本适合屏幕,java,android,android-layout,textview,Java,Android,Android Layout,Textview,我希望文本视图中的文本适合屏幕。我需要实现如下内容: 字母“A”需要位于屏幕中心,其高度等于设备屏幕的高度。我已经为此创建了一个自定义文本视图,如下所示,但这似乎不起作用。我的意思是,我的文本(字母A)不适合屏幕的高度。我试图手动调整文本字体大小,但我猜这不是正确的方法。有人能指出一个更好的解决办法吗 package com.example; import android.content.Context; import android.content.res.TypedArray;

我希望文本视图中的文本适合屏幕。我需要实现如下内容:

字母“A”需要位于屏幕中心,其高度等于设备屏幕的高度。我已经为此创建了一个自定义文本视图,如下所示,但这似乎不起作用。我的意思是,我的文本(字母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);
     }
   }
}
package.com.example;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.graphics.Paint;
导入android.graphics.Rect;
导入android.util.AttributeSet;
导入android.util.TypedValue;
导入android.widget.TextView;
公共类FontFitTextView扩展了TextView
{
私人涂料;
私有float maxFontSize;
私有静态最终浮动最大字体大小默认值=20f;
公共FontFitTextView(上下文)
{
超级(上下文);
初始化(上下文,空);
}
公共FontFitTextView(上下文上下文,属性集属性集)
{
super(上下文、属性集);
初始化(上下文、属性集);
}
公共FontFitTextView(上下文上下文、属性集、属性集、int-defStyle)
{
super(上下文、属性集、定义样式);
初始化(上下文、属性集);
}
私有void初始化(上下文上下文,属性集属性集)
{
if(attributeSet!=null)
{
TypedArray styledAttributes=上下文。获取styledAttributes(attributeSet,R.styleable.FontFitTextView);
maxFontSize=styledAttributes.getDimension(R.styleable.FontFitTextView\u maxFontSize,MAX\u FONT\u SIZE\u DEFAULT\u值);
styledAttributes.recycle();
}
其他的
{
maxFontSize=最大字体大小默认值;
}
mTestPaint=新油漆();
mTestPaint.set(this.getPaint());
//最大大小默认为初始指定的文本大小,除非它太小
}
私有文本(字符串文本、整型文本宽度、整型文本高度)
{
if(文本宽度阈值)
{
浮动大小=(高+低)/2;
mTestPaint.setTextSize(尺寸);
mTestPaint.getTextBounds(text,0,text.length(),bounds);
如果(bounds.width()>=targetWidth | | bounds.height()>=targetHeight)
hi=大小;//太大
其他的
lo=大小;//太小
}
//使用lo,使我们的下冲而不是过冲
此.setTextSize(TypedValue.COMPLEX\u UNIT\u PX,lo);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级)
{
超级测量(宽度测量、高度测量);
int parentWidth=MeasureSpec.getSize(widthmasurespec);
int height=getMeasuredHeight();
重新设置文本(this.getText().toString(),parentWidth,height);
此.setMeasuredDimension(父级宽度、高度);
}
@凌驾
受保护的void onTextChanged(最终字符序列文本、最终整数开始、最终整数之前、最终整数之后)
{
文本(text.toString()、this.getWidth()、this.getHeight());
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh)
{
如果(w!=oldw)
{
重新设置文本(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>


尝试调用
super.onMeasure()
方法,该方法位于
onMeasure()
方法的末尾,带有更新的宽度和高度(parentWidth,height)。

“但这似乎不起作用。”你能详细说明一下吗?我编辑了这个问题。基本上,我需要在文本视图中添加文本以适应屏幕的高度。目前它不适合设备屏幕高度Hi Naresh,我试过了。但是我看不到屏幕上的文本:(在调用super方法之前,请尝试调整宽度和高度值。传递给super方法的该值决定宽度和高度。parentWidth和height中的值可能太小或太大。请尝试对此进行调查和研究。您好,在Android中使用Log语句进行调试时,我得到了parentWidth为480,height为0“widthMeasureSpec”和“heightMeasureSpec”中的值是什么?它看起来像什么?意味着当您调用super.onMeasure(widthMeasureSpec,heightMeasureSpec)时,它是否会填满屏幕?不,它不会填充屏幕。它显示为普通文本。顺便说一句,我得到的值是widthMeasureSpec-->1073742304和heightMeasureSpec 1073742514。这是我在最后调用super.onMeasure(widthMeasureSpec,heightMeasureSpec)时得到的值