我正在 Android Studio(Mac OS) 中制作一个计算器应用程序,我试图在其中包含square(x^2) Button。我已将我的正方形设置为 TextView。
我已经尝试了所有提到的链接,但没有一个适合我以正确的公式格式输入它。
<TextView
android:id="@+id/square"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-20dp"
android:layout_marginStart="-5dp"
android:padding="15dp"
android:textColor="@color/black"
android:textSize="18sp" />
Run Code Online (Sandbox Code Playgroud)
我也在MainActivity.java文件中尝试了以下代码:-
squareButton = findViewById(R.id.square);
squareButton.setText(Html.fromHtml("x<sup>2</sup>"));
Run Code Online (Sandbox Code Playgroud)
PS我设置minSdkVersion 14 和targetSdkVersion 26!
Android 原生支持子/上标类型文本,称为SubscriptSpan和SuperscriptSpan
上标的示例用法(例如 X^2)
String text = "X2";
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.setSpan(
superscriptSpan,
text.indexOf("2"),
text.indexOf("2") + String.valueOf("2").length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(builder);
Run Code Online (Sandbox Code Playgroud)
我找到了一个很好的例子。
| 归档时间: |
|
| 查看次数: |
3261 次 |
| 最近记录: |