如何从另一个布局中包含的包含合并标记的布局访问视图? [英] How can I access views from layout containing merge tag is included in another layout?

查看:77
本文介绍了如何从另一个布局中包含的包含合并标记的布局访问视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android Studio 3.6-RC1并构建了3.6.0-rc01版的工具,并遇到了ViewBinding功能的问题:

I use Android Studio 3.6-RC1 and build tools version 3.6.0-rc01 and encountered an issue with ViewBinding feature:

我有带有以下标记的activity_test.xml文件:

I have activity_test.xml file with the following markup:

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

并带有以下标记的merge_view.xml:

And merge_view.xml with following markup:

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

活动代码如下:

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"

    }
}

问题是当我尝试从合并的布局访问TextView时,应用程序引发了异常,并显示消息 java.lang.NullPointerException:缺少ID为:viewMerged的必需视图.

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.

生成的绑定类如下所示:

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));
  }
}

我是否缺少某些东西,或者无法通过包含标签的布局访问视图,还是Android Studio 3.6-RC1中尚未提供该视图?

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?

推荐答案

我写了一篇有关将ViewBinding与< merge> 标记一起使用的文章,您可以在其中找到

I wrote an article about using ViewBinding with <merge> tag which you can find here

基本上,您需要做的是

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

例如,您具有 merge_view.xml ,因此将生成 MergeViewBinding 类,这是从此布局访问视图的方式.

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.

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"
    }
}

这篇关于如何从另一个布局中包含的包含合并标记的布局访问视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆