【问题标题】:How do I increase the text size in my android textview based on loaded text?如何根据加载的文本增加我的 android textview 中的文本大小?
【发布时间】:2016-06-21 18:35:50
【问题描述】:

我有一个显示从数据库加载的文本的文本视图。有时文本是单个字母,例如b,有时是两个超过两行的单词,例如small word

如果我显示单个字母,我希望字体大小更大。我尝试使用带有size 属性的html 字体标签并通过Html.fromHtml() 传递字符串:

<font size='6'>d</font>

但不幸的是,字体大小没有改变,Android 似乎无法识别该属性。

我不想在px 中提供字体大小,因为我有时想在 textviews 中加载具有不同默认文本大小的文本。


更多细节: 我有一个向用户询问一系列问题的应用程序。为此,它会在两个单独的textviews 中显示两个可能的答案。我的数据库中有几千个问题。该活动每次运行 3 分钟,我希望用户每三秒回答一次问题。

有时答案是一行字,有时是两行,有时是一个字母。如果是单个字母,我觉得以相同的文本大小显示字母是在浪费空间,所以我想增加它的文本大小。

在显示问题的字段中,我遇到了类似的问题。有时问题需要两行,有时需要一个简短的单行词。当需要一行时我也想增加字体大小。

【问题讨论】:

  • 您确实意识到TextViews 有一个 textSize 属性,对吧?无论如何,您可以测量屏幕上文本的大小,然后相应地调整 textSize。
  • @XaverKapeller :我知道我可以自己通过正则表达式读出标签,然后更改 textSize 但对我来说这看起来不是一个明显的解决方案,我觉得应该有一种更直接的方式。至于测量屏幕上的文本大小并动态更改文本大小,您可以将我发送到资源吗?
  • 你在说什么?正则表达式? TextView 本身有一个setTextSize() 方法。您可以使用它来设置文本大小,例如通过调用getTextBounds() 并将其与可用大小进行比较来根据文本的宽度。您还可以使用Paint 实例测量文本的宽度,这可能更可取。
  • 我建议您不要使用任何Spannables 来设置文本样式。他们从来没有工作得那么好。特别是Html.fromHtml()。从来没有理由使用它...除了一些非常特殊的情况。
  • 我想从数据库中加载字符串。关于 textsize 的知识包含在该字符串中。正则表达式将是一种解决方法。

标签: android text


【解决方案1】:

更新:

我创建了一个 CustomTextView 类,将 ondraw 方法动态覆盖为 setTextSize,而不是设置每个 TextView。

public class CustomTextView extends TextView {
  private static final String TAG = "CustomTextView";

  public CustomTextView(Context context) {
    super(context);
  }

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

  public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override protected void onDraw(Canvas canvas) {
    if (getLineCount() == 1) {
      setTextSize(40);
    } else {
      setTextSize(20);
    }
    super.onDraw(canvas);
  }
}

layout_custom.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="98dp"
    >
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:weightSum="1"
      >
    <com.example.blizzard.sof.CustomTextView
        android:id="@+id/customtextview1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:layout_weight="0.5"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center"
        android:textColor="@android:color/holo_red_dark"
        android:text="Word1 \nWord2"
        />

    <com.example.blizzard.sof.CustomTextView
        android:id="@+id/customtextview2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:layout_marginLeft="16dp"
        android:layout_weight="0.5"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center"
        android:textColor="@android:color/holo_red_dark"
        android:text="B"
        />
  </LinearLayout>
</LinearLayout>

确保将 com.example.blizzard.sof.CustomTextView 替换为适当的包名称。

预览:

上一个答案:

您可以使用AutoFitTextView 库。

将所有文本视图转换为AutofitTextView 并确保为属性android:textSize="?" & 设置适当的值 autofit:minTextSize="?".

示例:

【讨论】:

  • 在我看来,如果一行不适合所需的宽度,这个库会减小文本大小。另一方面,在我看来,如果文本的高度不适合 AutofitTextView 的高度,则文本会超出范围。
  • @Christian 您能否上传您的答案布局(.xml)以及它在设备中的外观以及您想要的外观的屏幕截图?
  • 我添加了一张图片。
  • @Christian 我已经更新了我的答案,你现在可以检查一下吗?
【解决方案2】:

如果我正确理解了您的问题,您希望根据显示的字母数来设置文本大小。

你可以这样做:

TextView textView = (TextView) findViewById (R.id.your_layout);
if (string.length() > 10) {
    textView.setTextSize(10);
else {
    textView.setTextSize(20);
}
textView.setText(string);

【讨论】:

    【解决方案3】:
    TextView textView = (TextView) findViewById (R.id.your_layout);
    textView.setTextSize(constant-textView.getText().toString().length);
    

    获取 TextView 文本,并根据长度设置相应的字体大小。例如 - 如果TextView 包含一个字母“b”,那么大小将为 constant - 1,如果它包含两个单词,如“Android Is Cool”,那么大小将为 constant - 15,比较小。

    【讨论】:

      【解决方案4】:

      你为什么不这样做

      textview.setText(“Some text”);
          textview.post(new Runnable() {
              @Override
              public void run() {
              if(textview.gettext().toString().length==1)
      {
      textView.setTextSize(10.0f);
      }else{
                  int lineCount = textview.getLineCount();
                  // Use lineCount here
      if(lineCount==1)
      {
      textView.setTextSize(20.0f);
      }else if(lineCount==2)
      {
      textView.setTextSize(30.0f);
      }
      }
              }
          });
      

      这应该可以让您根据文本控制文本大小。

      【讨论】:

        猜你喜欢
        • 2016-06-17
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        相关资源
        最近更新 更多