并非所有属性都可以在Android Studio中以编程方式更改



我有一个TextView对象,我想将其样式更改为Bold和其他一些属性。我做不到。它只是没有显示该选项,而我可以在xml文件中做。不确定我做错了什么。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="20dp">
<TableLayout
android:id="@+id/table_data"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:stretchColumns="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.037">
<TableRow
android:id="@+id/table_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nameRow"
android:layout_width="200dp"
android:gravity="center"
android:text="Name"
android:textColor="#000000"/>
<TextView
android:id="@+id/moneyRow"
android:gravity="center"
android:text="Karma"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

例如,name.text = key工作,但name.textStyle不工作。

感谢@CommonsWare的评论,我使用了Typeface来解决这个问题。

name.setTypeface(null, Typeface.NORMAL)  //removes the style.
name.setTypeface(null, Typeface.BOLD)  //make it bold.

最新更新