【问题标题】:How do I change the font size of TextView through a button?如何通过按钮更改 TextView 的字体大小?
【发布时间】:2020-09-03 19:04:08
【问题描述】:

我正在制作一个计算器应用程序,我正在尝试制作它,以便 CA(全部清除)按钮重置输入和输出空间中显示的文本的大小。因为我将输入和输出空间中的文本设置为根据等式和答案的长度自动调整大小,所以在下一次输入和输出时字体大小也会减小。有没有办法让按钮将文本恢复到原来的大小?

TextView 用于输入

<TextView
            android:layout_weight="1"
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:autoSizeTextType="uniform"
            android:autoSizePresetSizes="@array/text_sizes"
            android:maxLength="10"
            android:background="@color/black"
            android:fontFamily="@font/lgc"
            android:gravity="end"
            android:ellipsize="end"
            android:padding="20dp"
            android:text=""
            android:textColor="@color/white"
            android:textSize="30sp" />

【问题讨论】:

  • 我用的是安卓工作室

标签: java button text-size


【解决方案1】:

您可以在按钮点击事件下使用setTextSize(unit, size)

yourTextViewID.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);

参考Doc

【讨论】:

  • 即使我输入了原始尺寸值,它似乎也不起作用
  • btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { input.setText(""); output.setText(""); input.setTextSize(TypedValue .COMPLEX_UNIT_SP, 30); output.setTextSize(TypedValue.COMPLEX_UNIT_SP, 70); } });
  • TextView mytextView = (TextView) findViewById(R.id.yourTextViewID); mytextView .setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);
  • 字体大小仍然减少,我在主帖中包含了 TextView 的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2017-10-25
  • 2018-02-14
相关资源
最近更新 更多