这是我的自定义视图.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.viewModel.ViewModel" />
</data>
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.views.MyView
app:length="@{viewModel.codeLenght}"
app:success_color="@drawable/border_green_fill_white"
app:width="@dimen/icon_size_huge" />
</RelativeLayout>
这是我的观点:
class MyView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr){
init {
val styles = context.obtainStyledAttributes(attrs, R.styleable.CodeTextView)
styleEditTexts(styles, attrs)
styles.recycle()
}
private fun styleEditTexts(styles: TypedArray, attrs: AttributeSet?) {
length = styles.getInt(R.styleable.AttrView_length, DEFAULT_LENGTH)
generateViews(styles, attrs)
}
@BindingAdapter("app:length")
fun setLength(view: CodeTextView, size: Int) {
view.length = size
}
如何从ViewModel获取长度值
答案 0 :(得分:0)
您可以使用implementation files('core/libs/Java-WebSocket-1.3.7.jar')
implementation files('core/libs/javax.jmdns_3.4.1-patch2.jar')
,以与ViewModel
/ MyView
内部相同的方式在Activity
内部获得Fragment
类。
context