我在项目中使用数据绑定,并且按预期方式工作。
现在,我想使用<include>
标签并访问所包含布局中的字段,但无法使其正常工作。
我尝试了一个在网络上多次发现的代码段,如下所示:
hello_world.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include
android:id="@+id/included"
layout="@layout/included_layout"/>
</LinearLayout>
</layout>
included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/world"/>
</layout>
HelloWorldBinding binding =
HelloWorldBinding.inflate(getLayoutInflater());
binding.hello.setText("Hello");
binding.included.world.setText("World");
此代码不起作用。
当我在android studio中查看时,binding.included
是一个视图,因此无法访问字段。
binding.hello.setText("Hello");
正常工作,因此数据绑定至少在某种程度上有效。
如何访问所包含布局中的字段?
答案 0 :(得分:0)
将包含的布局(included_layout.xml)包装在ViewGroup中。这样可以解决问题。 included_layout.xml
应该看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/world"/>
</RelativeLayout>
</layout>
因此,当您要设置TextView的Text时,可以执行以下操作:
binding.included.world.setText("World");
每次使用include
标记时,include
标记都会用作所包含视图(应该是ViewGroup)中最外面的布局的占位符。因此,在使用include
标签时,所包含布局中的最外面的视图必须始终是ViewGroup。
我希望这有帮助。快活的编码!
答案 1 :(得分:0)
只需确保不要在id
和包含的布局上设置include
(两个都只能有一个id
)。包括的布局可能需要一个<data />
节点,该节点可能为空(当不从父布局传递变量时,就像我上面链接的一个答案一样)。
类似这样:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include
layout="@layout/included_layout"/>
</LinearLayout>
</layout>
这:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/world"/>
</layout>
答案 2 :(得分:-2)
删除该布局标签
included_layout.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/world"/>
然后尝试binding.included.world.setText("World")
更近的例子
class NavigationActivity : AppCompatActivity() {
private lateinit var activityNavigationBinding:ActivityNavigationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityNavigationBinding = DataBindingUtil.setContentView(
this,
R.layout.activity_navigation
)
activityNavigationBinding.includeLayout.tv_textView.setText("Sample text")
}
}
activity_navigation
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="@+id/includeLayout"
layout="@layout/app_bar_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_navigation"
app:menu="@menu/activity_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</layout>
app_bar_navigation
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.NavigationActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_textView"
/>
</android.support.design.widget.CoordinatorLayout>
之后,您需要像 bindingObject。(包含布局的ID)。(视图的ID).setText或其他任何东西