【问题标题】:How to access view inside a included layout using data binding如何使用数据绑定访问包含的布局内的视图
【发布时间】:2016-01-20 22:38:04
【问题描述】:

我有 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>

和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>

现在我在 Java 中

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

现在我想以

的身份访问 content_main 中的 textview
binding.content.textView

我尝试在布局标签中包含 content_main,但它不起作用。 我也关注了这个link,但它没有用

我该怎么做?

【问题讨论】:

  • 在您提到的链接中,提到使用版本 1.0-rc4 它应该可以在不传递假变量的情况下正常工作,但您需要在两个文件中都有根 &lt;layout&gt; 标记.我在你的 content_main.xml 布局中没有看到这个标签。
  • 谢谢@maciej,你说得对,我在原始代码中确实有布局标签,但我通过重建项目解决了这个问题

标签: android android-layout android-databinding


【解决方案1】:

检查一下

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>

在你的 java 文件中

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

包含文件的模式遵循与视图相同的模式:标签的 ID 用作其在类中的字段名称。包含的布局已经生成了自己的类,并在其布局中为视图生成了自己的字段。

如您所见。Source

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多