您好,我正在為我的大學移動設備技術開發一個 android 應用程式。在考試中,應用程式與 REST API 對話,該 API 在特定路線上提供一些資料作為回報。因此,我有必要使用從 API 為每個用戶獲取的資料在現場動態創建 UI 元素。
因此,我正在 Kotlin 中創建通用 UI 元素,一旦用戶在應用程式的生命周期中達到這一點,就可以通過編程方式將其添加到我的活動中。
通用 UI 元素的組成如下:
包含一個包含 TextView 的 ConstraintLayout 的 CardView
我在設定前兩個元素的引數方面有點掙扎,現在我被困在最后一個元素上,因為我找不到設定屬性的方法,所以無法正確設定:android:fontFamily="sans-serif-medium"
我在 Android Studio 的設計器工具中為我需要在 Kotlin 中重新創建的代碼創建了一個參考,如下所示:
<TextView
android:id="@ id/placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="sans-serif-medium"
android:text="@string/No_projects"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
這個 XML 等于 android studio 設計工具中宣告的引數。

我在 Kotlin 中設定 textStyle 和 typeface 屬性時遇到了困難,但在這里找到了(可能)解決方案
相反,這里有代碼片段,我在其中創建并一一設定 image/xml 片段中顯示的所有引數:
val genericTextView = TextView(context)
genericTextView.layoutParams.height = ConstraintLayout.LayoutParams.WRAP_CONTENT
genericTextView.layoutParams.width = ConstraintLayout.LayoutParams.WRAP_CONTENT
(genericTextView.layoutParams as ViewGroup.MarginLayoutParams).setMargins(0, 8, 0, 8)
genericTextView.id = generateViewId()
genericTextView.tag = "NoProjectsText"
genericTextView.visibility = TextView.VISIBLE
genericTextView.text = R.string.No_projects.toString()
genericTextView.textSize = 20f
genericTextView.setTextColor(context.getColor(R.color.black))
genericTextView.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
genericTextView.setTextAppearance(com.google.android.material.R.style.TextAppearance_AppCompat_Medium)
genericTextView.setTypeface(genericTextView.typeface, android.graphics.Typeface.BOLD)
// Constraints for inner text for elements cards
val connections2 = genericTextView.layoutParams as ConstraintLayout.LayoutParams
connections2.apply {
connections2.endToEnd = ConstraintSet.PARENT_ID
connections2.topToTop = ConstraintSet.PARENT_ID
connections2.startToStart = ConstraintSet.PARENT_ID
connections2.bottomToBottom = ConstraintSet.PARENT_ID
connections2.horizontalBias = 0.5f
connections2.verticalBias = 0.5f
}
我在類中找不到TextView修改或設定字體系列的屬性。
我希望我已經正確設定了其余的引數。
感謝您的任何幫助或建議。
uj5u.com熱心網友回復:
一個簡單的解決方案(有點曲折)是創建一個布局,其中包含 xml 中所有樣式的 TextView 并在代碼中呼叫它。看看下面
layout_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="sans-serif-medium"
android:text="@string/No_projects"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
MainActivity.kt
val inflater = LayoutInflater.from(this)
val tv = inflater.inflate(R.layout.layout_text_view, null, false) as TextView
tv.text = "Some text"
tv.visibility = View.VISIBLE
uj5u.com熱心網友回復:
一般來說,我不確定是否可以在代碼中執行此操作。有諸如Typeface.CustomFallbackBuilder之類的類允許您構造 aFontFamily并將其應用于 a Typeface,但這是最近在 API 29 中添加的。FontFamily類本身也是如此!
字體系列是在 Android 生命后期添加的,據我所知,這里是添加 XML 字體時的檔案- 其中包括在 XML 中定義字體系列。就像那里所說的那樣,這是在 8.0 中添加到 Android 本身的 - 該功能通過支持庫向后移植到較舊的 API。
所以我假設當你font-family在 XML 中定義一個屬性時,實際發生的是很多幕后作業,決議資源以選擇合適的字體(可能在編譯時),而不是像FontFamily剛剛創建的那樣整潔的類!
根據我自己的實驗,即使是這種向后移植的功能也有限制,Android 本身在以后的 API 中沒有 - 比如忽略除400和(我認為)700之外的所有權重,并且只選擇家族中最接近這些的字體(有時甚至如果重量更近,則在正常和斜體之間切換)
整個樣式/主題方面對我來說總是有點復雜,因此您可以為每個字體系列定義一個樣式,然后在運行時將其中一個應用為TextAppearance/Style/Theme/ThemeOverlay。我不確定,但可能需要調查一下。
但是對于您的示例, ,實際上這不是字體系列,它是sans-serif字體系列中的sans-serif-medium特定字體。所以也許你根本不需要字體系列?如果您可以將其轉換為or或其他內容,您可以輕松地在代碼中使用它并從中創建一個應用到您的. 我不知道這對你正在做的事情有多容易,但我假設你在這里使用一組已知的字體系列。sans_serif_500.ttfTypeface.SANS_SERIFTypefaceTextView
抱歉,這有點含糊,如果不是很明顯,我不確定自己有多少作業,但希望它可以為您指明正確的方向!過去我自己也遇到過問題,font-family它忽略了權重并造成不一致,即使材質型別系統使用常規和粗體以外的權重。我發現為每個粗細創建字體系列(假設它們是 400)并在樣式中明確參考它們更容易——而且我只需要一個系列,這樣我就可以將它們與斜體變體聯系起來。如果您也需要,我會研究“應用主題”的想法,看看您是否可以在運行時覆寫一些樣式(使用定義字體系列的 XML 資源)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/514412.html
標籤:安卓科特林
