[英]How to create Android UI library
我有很多用於 UI 的實用代碼,想為每個代碼創建一個 android 庫。 通過我所有的研究,我做了以下事情。
文件 -> 新模塊。 創建一個 android 庫
在 build.gradle 文件中實現庫implementation project(':app:FirstLibrary')
庫中的 Kotlin 和 xml 文件如下
FirstLib.kt
class FirstLib(mContext: Context) : Activity() {
private var mContext: Context? = null
private var view: View? = null
fun s(message: String?) {
layoutInflater.inflate(R.layout.first_lib_view, null)
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show()
}
}
first_lib_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Om Namah Shivaya"
android:textSize="16sp" />
</LinearLayout>
<com.example.firstlibrary.FirstLib
android:layout_width="match_parent"
android:layout_height="match_parent"
tools="ajkdfbj"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
現在我什么也得不到。 甚至吐司消息都沒有來。
Android中如何創建UI庫並使用。 我有自己的自定義視頻播放器、音頻播放器、圖像漸變視圖等。我現在需要制作所有這些庫。 謝謝閱讀
要實現自定義視圖元素,您需要像以前一樣將 class 放入單獨的模塊中,並從視圖中繼承您的自定義視圖。
xml
文件中使用它,那么您應該創建如下內容:MyCustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): View {
// you should override onMeasure, onLayout, onDraw here as it marked as abstract afaik
// and implement your logic
}
並實現整個onMeasure
、 onLayout
、 onDraw
邏輯。
MyCustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): TextView() {
... your logic goes here...
}
您確實實現了自定義活動,但像使用視圖一樣使用它,所以,這就是它對您不起作用的原因
PS:在這里你可以找到更多關於自定義視圖組件的信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.