我正在尝试通过编程创建的TextView
中的LinearLayout
使用最新版本的FontAwesome字体,但是显示了一个包含十字的框。我同时尝试了FontManager.getTypeface(activity!!.baseContext, FontManager.FONTAWESOME)
和FontManager.getTypeface(activity!!.applicationContext, FontManager.FONTAWESOME)
,但都没有用。我什至在使用FontAwesone提供的最新.otf
文件。我真的不明白自己做错了什么,因为我遵循了FontAwesome网站上的所有说明。是因为布局是动态创建的还是其他问题?
FontManager对象
object FontManager {
private const val ROOT = "fonts/"
internal const val FONTAWESOME = ROOT + "fontawesome5-free-regular.otf"
internal fun getTypeface(context: Context, font: String): Typeface {
return Typeface.createFromAsset(context.assets, font)
}
}
片段类
class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mainRelativeLayout = RelativeLayout(activity)
// Layout Parameters for relative layout items
val rlpCVPrimary = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVPrimaryIcon = RelativeLayout.LayoutParams(LinearLayout.LayoutParams
.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVPrimaryText = RelativeLayout.LayoutParams(LinearLayout.LayoutParams
.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVTitle = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpCVSecondary = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVSecondaryIcon = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVSecondaryText = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
// Accessing FontAwesome font
val iconFont = FontManager.getTypeface(activity!!.applicationContext, FontManager.FONTAWESOME)
//
val r = context!!.resources
val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()
val thirtyDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, r.displayMetrics).toInt()
// Creating RelativeLayouts for root layout
val cvRLPrimary = RelativeLayout(context)
val cvRLSecondary = RelativeLayout(context)
// Creating CardViews
val cvPrimary = context?.let { CardView(it) }
when {
cvPrimary != null -> {
cvPrimary.radius = fourDp.toFloat()
cvPrimary.setContentPadding(tenDp,tenDp,tenDp,tenDp)
cvPrimary.useCompatPadding = true
cvPrimary.cardElevation = fourDp.toFloat()
cvPrimary.layoutParams = rlpCVPrimary
}
}
rlpCVPrimary.setMargins(0, 0, 0, thirtyDp)
val cvSecondary = context?.let { CardView(it) }
when {
cvSecondary != null -> {
cvSecondary.radius = fourDp.toFloat()
cvSecondary.setContentPadding(tenDp,tenDp,tenDp,tenDp)
cvSecondary.useCompatPadding = true
cvSecondary.cardElevation = fourDp.toFloat()
cvSecondary.layoutParams = rlpCVSecondary
}
}
rlpCVSecondary.setMargins(0, 0, 0, thirtyDp)
/*
* Creating Primary CardView items
*/
// 'tvPrimaryIcon' TextView
val tvPrimaryIcon = TextView(context)
tvPrimaryIcon.text = getString(R.string.fa_icon_sign_in)
tvPrimaryIcon.typeface = iconFont
tvPrimaryIcon.layoutParams = rlpCVPrimary
tvPrimaryIcon.setMargins(0, 0, tenDp, 0)
TextViewCompat.setTextAppearance(tvPrimaryIcon, android.R.style.TextAppearance_Medium)
// 'tvPrimaryText' TextView
val tvPrimaryText = TextView(context)
tvPrimaryText.text = getString(R.string
.primary_title)
TextViewCompat.setTextAppearance(tvPrimaryText, android.R.style.TextAppearance_Medium)
// 'tvTitle' TextView
val tvTitle = TextView(context)
tvTitle.text = getString(R.string.hello_world)
tvTitle.gravity = Gravity.CENTER
TextViewCompat.setTextAppearance(tvTitle, android.R.style.TextAppearance_Medium)
rlpTVTitle.setMargins(0, tenDp, 0, tenDp)
/*
* Creating Secondary CardView items
*/
// 'tvSecondaryIcon' TextView
val tvIconExit = TextView(context)
tvIconExit.text = getString(R.string.fa_icon_sign_out)
tvIconExit.typeface = iconFont
TextViewCompat.setTextAppearance(tvIconExit, android.R.style.TextAppearance_Medium)
rlpTVIconExit.setMargins(0, 0, tenDp, 0)
// 'tvSecondaryText' TextView
val tvSecondaryText = TextView(context)
tvSecondaryText.text = getString(R.string.secondary_title)
TextViewCompat.setTextAppearance(tvSecondaryText, android.R.style.TextAppearance_Medium)
// Set IDs for text views
mainRelativeLayout.id = View.generateViewId()
cvPrimary!!.id = View.generateViewId()
tvPrimaryIcon.id = View.generateViewId()
tvPrimaryText.id = View.generateViewId()
tvTitle.id = View.generateViewId()
cvSecondary!!.id = View.generateViewId()
tvSecondaryIcon.id = View.generateViewId()
tvSecondaryText.id = View.generateViewId()
// Set Layout Parameters for text views
tvPrimaryIcon.layoutParams = rlpTVPrimaryIcon
tvPrimaryText.layoutParams = rlpTVPrimaryText
tvTitle.layoutParams = rlpTVTitle
tvSecondaryIcon.layoutParams = rlpTVSecondaryIcon
tvSecondaryText.layoutParams = rlpTVSecondaryText
// Set RelativeLayout rules for views
rlpTVPrimaryText.addRule(RelativeLayout.END_OF, tvPrimaryIcon.id)
rlpTVTitle.addRule(RelativeLayout.BELOW, cvPrimary.id)
rlpCVSecondary.addRule(RelativeLayout.BELOW, tvTitle.id)
rlpTVSecondaryText.addRule(RelativeLayout.END_OF, tvSecondaryIcon.id)
// Adding items to root layout
mainRelativeLayout.addView(cvPrimary)
cvPrimary.addView(cvRLPrimary)
cvRLPrimary.addView(tvPrimaryIcon)
cvRLPrimary.addView(tvPrimaryText)
mainRelativeLayout.addView(tvTitle)
mainRelativeLayout.addView(cvSecondary)
cvSecondary.addView(cvRLSecondary)
cvRLSecondary.addView(tvSecondaryIcon)
cvRLSecondary.addView(tvSecondaryText)
return mainRelativeLayout
}
}