[英]How to use View Binding with Included Views?
视图绑定随 v3.6 发布。
文档: https : //developer.android.com/topic/libraries/view-binding
我的问题是,有谁知道如何使用包含布局的视图绑定?
包含另一个布局的给定布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/my_header"
layout="@layout/item_header"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
我正在尝试引用 item_header 布局内的项目。
binder.my_header (<-- this just returns back the view)
binder.root (<-- this just returns back the root view)
即使我向 item_header 的根添加一个 id,例如 id="@+id/parent_id" 并尝试引用它,我也会收到空指针异常
binder.parentId (<-- I have access to views inside of the item_header, however, I receive exceptions. Says that "parentId" cannot be found)
如何引用布局item_header
?
假设您问题中的布局是activity_main.xml
。 为它生成的视图绑定类是ActivityMainBinding
。 同样,对于item_header.xml
,生成的视图绑定是ItemHeaderBinding
。
如果我们假设item_header.xml
有一个名为@+id/foo
的TextView
,那么你最终会得到这个 Kotlin 代码块:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
mainBinding.myHeader.foo.text = "this is a test"
}
}
因此, ActivityMainBinding
对象应该有一个属性,其中包含您提供给<include>
的android:id
— 在本例中为myHeader
。 那应该是一个ItemHeaderBinding
,因为视图绑定似乎为<include>
设置了嵌套的绑定对象。 由于myHeader
是ItemHeaderBinding
,因此您可以在其上引用小部件,就像您自己直接膨胀ItemHeaderBinding
一样。
请注意,视图绑定似乎将lower_snake_case
转换为lowerCamelCase
,因此就生成的代码而言, my_header
ID 变成了myHeader
。
我已经在 Java 中检查过这个问题。
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/my_header"
layout="@layout/item_header"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
item_header.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"
android:clickable="false"
android:background="@android:color/transparent">
<TextView
android:id="@+id/foo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
主活动.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.myHeader.foo.setText("this is a test");
}
}}
我检查了它在我的新项目中是否有效。 我希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.