在使用视图绑定时,我遇到了一些未记录的情况。

第一:我如何获得绑定包含视图布局部分?主绑定只能看到在主布局中定义的项。

第二:如何获得合并布局部分的绑定。同样,主绑定只看到主布局中的项目?


当前回答

假设我在activity_main.xml文件中包含了如下布局:

<include
    android:id="@+id/ll_layout1"
    layout="@layout/layout1"
    android:visibility="gone" />

假设我想改变它的可见性。我可以这样做:

activityMainBinding.llLayout1.root.visibility = View.VISIBLE

其他回答

在下列情况下:

包含与泛型布局(不是合并节点),我们需要分配ID到包含的部分,这样在绑定中我们就可以访问包含的子部分

<include
    android:id="@+id/your_id"
    layout="@layout/some_layout" />

在你的活动代码中这样做:

private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    setContentView(exampleBinding.root)
    //we will be able to access included layouts view like this
    val includedView: View = exampleBinding.yourId.idOfIncludedView
//[...]
}

在外部布局中包含合并块。我们不能添加ID,因为merge block不是视图。 假设我们有这样一个永恒的合并布局(merge_layout.xm):

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/activity_example">

    <TextView
        android:id="@+id/some_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />
</merge>

要正确绑定这样的合并布局,我们需要:

在你的活动代码中:

private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout
private lateinit var mergeBinding: MergeLayoutBinding  //merge_layout.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    //we need to bind the root layout with our binder for external layout
    mergeBinding = MergeLayoutBinding.bind(exampleBinding.root)
    setContentView(exampleBinding.root)
    //we will be able to access included in merge layout views like this
    val mergedView: View = mergeBinding.someView
//[...]
}

假设我在activity_main.xml文件中包含了如下布局:

<include
    android:id="@+id/ll_layout1"
    layout="@layout/layout1"
    android:visibility="gone" />

假设我想改变它的可见性。我可以这样做:

activityMainBinding.llLayout1.root.visibility = View.VISIBLE

在我的例子中,我忘记将id分配给include标记

现在,当你赋值了id,你就能得到绑定对象,

YourMainLayoutBinding.YourIncludeTagIDLayoutBinding

使用数据绑定库。然后用<layout>标签包装XML布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    ... 

    <include 
      android:id="@+id/toolbar"
      layout="@layout/toolbar" />
    
    ...

</LinearLayout>
</layout>

toolbar.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"
    android:orientation="vertical">

    <ImageView 
        android:id="@+id/ivImage"
        ... />

    <TextView 
        android:id="@+id/tvTitle"
        ... />


</LinearLayout>

MainActivity.kt

private lateinit var binding: ActivityMainBinding  

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    // Access include layout views
    binding.toolbar.rootView.ivImage.setImageResource(R.drawable.ic_back_arrow)
    binding.toolbar.rootView.tvTitle.text = getString(R.string.home)
   
    ...
}

遵循步骤:

private val binding: FragmentBinding 由viewBinding (FragmentBinding:绑定) 确保在“onViewCreated(view: view, savedInstanceState: Bundle?)”中执行以下操作 Val binding2 = binding.root.include_layout_id

例如val binding2 = binding.root.tool_bar_layout

现在访问你的include布局,视图。如。

binding2.textView.text = "your text"