我正在努力定义如下的自定义DialogFragment布局:
<?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"
android:layout_width="match_parent"
android:layout_height="360dp"
android:background="@color/colorDarkerGreen"
android:minWidth="280dp"
android:orientation="vertical">
<ImageView
android:id="@+id/ivNoRetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:scaleType="centerInside"
android:src="@drawable/no_retails_nearby_dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:text="@string/no_retails_nearby"
android:textAlignment="center"
android:textColor="@color/colorBaseWhite"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivNoRetails" />
<TextView
android:id="@+id/tvHopeGetThere"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="@string/hope_we_get_there"
android:textAlignment="center"
android:textColor="@color/colorBaseWhite"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/textView2"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btClose"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:text="@string/understood"
android:textColor="@color/colorBaseWhite"
app:backgroundTint="@android:color/transparent"
app:cornerRadius="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvHopeGetThere"
app:strokeColor="@color/colorBaseWhite"
app:strokeWidth="2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
从布局编辑器中预览,我可以看到tvHopeGetThere
TextView的文本显示为粗体,如预期的那样。
但是,当我在活动中显示对话框时,该文本将显示为具有常规textStyle的样式。
DialogFragment:
class NoRetailsNearbyDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.no_retails_alert_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btClose.setOnClickListener {
dismiss()
}
}
companion object {
fun newInstance(): NoRetailsNearbyDialogFragment {
return NoRetailsNearbyDialogFragment()
}
}
}
在我的片段中显示对话框:
val fragment = NoRetailsNearbyDialogFragment.newInstance()
fragment.show(fragmentManager, "")
答案 0 :(得分:0)
有一种方法,您可以通过调用setTypeface方法来设置textview字体, 见以下 您可以通过以下方式进行编程:
textView.setTypeface(null, Typeface.BOLD);
或者您也可以通过html方法来做到这一点:
TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));
答案 1 :(得分:0)
问题与Calligraphy库有关,该库覆盖了常见的textStyle行为。一旦删除它,一切就可以正常工作了:)