简体   繁体   English

如何从包含合并标记的布局访问视图包含在另一个布局中?

[英]How can I access views from layout containing merge tag is included in another layout?

I use Android Studio 3.6-RC1 and build tools version 3.6.0-rc01 and encountered an issue with ViewBinding feature:我使用 Android Studio 3.6-RC1 和构建工具版本 3.6.0-rc01 并遇到了 ViewBinding 功能的问题:

I have activity_test.xml file with the following markup:我有带有以下标记的 activity_test.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/view_merged"
        layout="@layout/merge_view" />
</LinearLayout>

And merge_view.xml with following markup:和 merge_view.xml 带有以下标记:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Merge view" />
</merge>

Activity code looks like the following:活动代码如下所示:

class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.viewMerged.label.text = "New text"

    }
}

Problem is when I try to access TextView from merged layout, the app throws an exception with the message java.lang.NullPointerException: Missing required view with ID: viewMerged.问题是当我尝试从合并布局访问 TextView 时,该应用程序抛出异常并显示消息java.lang.NullPointerException: Missing required view with ID: viewMerged.

The generated binding class looks like the following:生成的绑定类如下所示:

public final class ActivityTestBinding implements ViewBinding {
  @NonNull
  private final LinearLayout rootView;

  @NonNull
  public final MergeViewBinding viewMerged;

  private ActivityTestBinding(@NonNull LinearLayout rootView,
      @NonNull MergeViewBinding viewMerged) {
    this.rootView = rootView;
    this.viewMerged = viewMerged;
  }

  @Override
  @NonNull
  public LinearLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityTestBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityTestBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_test, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityTestBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    String missingId;
    missingId: {
      View viewMerged = rootView.findViewById(R.id.view_merged);
      if (viewMerged == null) {
        missingId = "viewMerged";
        break missingId;
      }
      MergeViewBinding viewMergedBinding = MergeViewBinding.bind(viewMerged);
      return new ActivityTestBinding((LinearLayout) rootView, viewMergedBinding);
    }
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

Am I missing something or there is no way to access views from included layouts with tags or it is not yet shipped in Android Studio 3.6-RC1?我是否遗漏了什么,或者无法从包含标签的布局访问视图,或者它尚未在 Android Studio 3.6-RC1 中提供?

I wrote an article about using ViewBinding with <merge> tag which you can find here我写了一篇关于使用带有<merge>标签的 ViewBinding 的文章,你可以在这里找到

Basically, what you need to do is基本上,你需要做的是

  • Don't give <include> tag any ID.不要给<include>标记任何 ID。
  • Call bind() method of generated merge layout binding, passing the root view of the layout you included your layout in.调用生成的合并布局绑定的bind()方法,传递包含布局的布局的根视图。
  • Access your view from the object of merge binding从合并绑定的对象访问您的视图

For example, you have merge_view.xml so you'll have MergeViewBinding class generated and this is how you will access the view from this layout.例如,您有merge_view.xml因此您将生成MergeViewBinding类,这就是您从此布局访问视图的方式。

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityTestBinding.inflate(layoutInflater)
        val mergeBinding = MergeViewBinding.bind(binding.root)
        setContentView(binding.root)
        mergeBinding.label.text = "New text"
    }
}

Simply use the binding of the view/file.只需使用视图/文件的绑定。 Here is an example:下面是一个例子:

import ch.zkb.mobile.bank.databinding.MyFragmentBinding
import ch.zkb.mobile.bank.databinding.MyMergeBinding

class MyFragment : (R.layout.my_fragment) {

    private val binding by viewBinding(MyFragmentBinding::bind)
    private val mergeBinding by viewBinding(MyMergeBinding::bind)

    ...
}

View example:查看示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/my_merge" />

</LinearLayout>

Make sure, you build it first, so the Binding is generated.确保首先构建它,以便生成绑定。

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

相关问题 如何从包含的布局访问按钮代码? - How to access the button code from an included layout? 如何将包含的 xml 布局更改为 Java 代码上的另一个布局? - How can i change included xml layout to another layout on java code? 如何访问页脚xml布局内的视图? 安卓系统 - How can I access the views inside of a footer xml layout? Android 无法访问包含布局内的视图 - Cannot Access Views inside Included layout 如何在线性布局(另一个线性布局的子级)中均匀分布视图? - How can I distribute views in a linear layout (which is a child of another linear layout) evenly? 如何在布局中为另一个片段使用片段xml标签? - How can i use fragment xml tag in layout for another fragment? 如何防止视图在相对布局中重叠? - How can I prevent views from overlapping in relative layout? 修改包含在另一个布局中的布局中的视图,使用<include>标签</include> - Modify View in a layout included in another layout using <include> tag 如何在相对布局或任何布局中动态添加视图 - How can I add Views dynamically in a Relative layout or in any Layout 如何在布局中包含 listView 以及布局中的其他视图 - How can I include listView in a layout with other views in the layout already
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM