简体   繁体   English

如何使用数据绑定访问包含的布局中的视图

[英]How to access view inside a included layout using data binding

I have content_main layout 我有content_main布局

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> 

and activity_main 和activity_main

 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" android:id="@+id/content" app:foo="@{1}"> </include> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> </layout> 

Now in the Java I have 现在在Java中我有

ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);

Now I want to access the textview inside the content_main as 现在我想访问content_main中的textview作为

binding.content.textView

I tried having content_main in a layout tag but it did not work. 我尝试在布局标记中使用content_main但它不起作用。 I also followed this link but it did not work 我也按照这个链接但它没有用

How can I do that? 我怎样才能做到这一点?

Check this 检查一下

hello_world.xml 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 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>

In your java file 在你的java文件中

HelloWorldBinding binding =
    HelloWorldBinding.inflate(getLayoutInflater());
binding.hello.setText(“Hello”);
binding.included.world.setText(“World”);

The pattern for included files follows the same pattern as for Views: the ID of the tag is used as its field name in the class. 包含文件的模式遵循与视图相同的模式:标记的ID用作类中的字段名称。 The included layout has generated its own class with its own fields for the Views in its layout. 包含的布局已生成自己的类,其布局中的视图具有自己的字段。

Simple as you see. 你看,简单。 Source 资源

我通过重建项目解决了这个问题,你也应该在content_main XML中使用layout标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android数据绑定。 如何通过包含布局内的ID访问视图 - android data binding. How to access view via ID inside a included layout 如何访问android xml布局文件中包含的视图内的元素? - how to access elements inside an included view in a android xml layout file? 如何使用Kotlins视图绑定访问另一个类内的视图 - How to access a view inside of another class using Kotlins view binding 包含 xml 布局的访问视图 - Access view of included xml layout 无法使用数据绑定在包含的布局中传递颜色变量 - Unable to pass color variable in included layout using Data Binding 使用数据绑定,如何将函数(或 lambda)变量传递给包含的布局 - With data binding, How to pass function(or lambda) variable to included layout 如何使用 android 合成的 kotlin 从包含的布局访问视图 - How to access view from included layout with kotlin synthetic in android Android 数据绑定不适用于包含的布局 - Android Data binding is not working for included layout 如何使用数据绑定和导航组件访问嵌套视图? - How to access to nested view using data binding and navigation component? 无法访问包含布局内的视图 - Cannot Access Views inside Included layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM