ham*_*ham 6 android android-viewbinding
我无法获得带有合并根标记的布局来处理视图绑定。是否可以?
include_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/include_test"
layout="@layout/merge_layout"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
合并布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/include_test">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A Title"/>
</merge>
Run Code Online (Sandbox Code Playgroud)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = IncludeLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.includeTest.title.text = "New Title"
}
Run Code Online (Sandbox Code Playgroud)
运行时异常:
java.lang.NullPointerException: Missing required view with ID: includeTest
根据这篇文章,这是可能的
例如 :
my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/my_include_layout"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
my_include_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</merge>
Run Code Online (Sandbox Code Playgroud)
MyFragment.kt
class MyFragment: Fragment() {
private val binding: MyFragmentBinding get() = _binding!!
private var _binding: MyFragmentBinding? = null
private val myIncludeLayoutBinding: MyIncludeLayoutBinding get() = _myIncludeLayoutBinding!!
private var _myIncludeLayoutBinding: MyIncludeLayoutBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = MyFragmentBinding.inflate(inflater, container, false)
_myIncludeLayoutBinding = MyIncludeLayoutBinding.bind(binding.root)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
myIncludeLayoutBinding.button.setOnClickListener{
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
_myIncludeLayoutBinding = null
}
}
Run Code Online (Sandbox Code Playgroud)
arb*_*erg -2
不支持。
请参阅此处的官方文档:
https://developer.android.com/topic/libraries/data-binding/expressions
编辑:哎呀,这些文档讨论的是数据绑定,而不是视图绑定。
| 归档时间: |
|
| 查看次数: |
2766 次 |
| 最近记录: |