首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视图绑定--如何获得包含的布局的绑定?

视图绑定--如何获得包含的布局的绑定?
EN

Stack Overflow用户
提问于 2019-11-06 12:23:38
回答 8查看 63.2K关注 0票数 205

在使用视图绑定时,我遇到了一些没有文档的案例。

第一:如何为包含的视图布局部件绑定?主绑定只看到主布局中定义的项。

第二:如何为合并的布局部件绑定。同样,主绑定只看到主布局中的项目?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2019-11-06 12:23:38

在下列情况下:

  1. 包含有通用布局(而不是合并节点),我们需要为包含的部件分配ID,这样在绑定中我们就可以访问包含的子部件

代码语言:javascript
复制
<include
    android:id="@+id/your_id"
    layout="@layout/some_layout" />

这样在您的活动代码中:

代码语言:javascript
复制
private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    setContentView(exampleBinding.root)
    //we will be able to access included layouts view like this
    val includedView: View = exampleBinding.yourId.idOfIncludedView
//[...]
}

  1. 包含在外部布局中的合并块。我们不能向它添加ID,因为合并块不是视图。假设我们有这样一个永恒的合并布局(merge_layout.xm):

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<merge 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"
    tools:showIn="@layout/activity_example">

    <TextView
        android:id="@+id/some_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />
</merge>

要正确绑定这样的合并布局,我们需要:

在您的活动代码中:

代码语言:javascript
复制
private lateinit var exampleBinding: ActivityExampleBinding  //activity_example.xml layout
private lateinit var mergeBinding: MergeLayoutBinding  //merge_layout.xml layout

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    exampleBinding = ActivityExampleBinding.inflate(layoutInflater)
    //we need to bind the root layout with our binder for external layout
    mergeBinding = MergeLayoutBinding.bind(exampleBinding.root)
    setContentView(exampleBinding.root)
    //we will be able to access included in merge layout views like this
    val mergedView: View = mergeBinding.someView
//[...]
}
票数 298
EN

Stack Overflow用户

发布于 2020-03-10 11:41:24

关于第一个问题,您可以获得包含的布局的视图绑定。

下面是一个示例main_fragment.xml文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

</LinearLayout>

MainFragment.java可以是这样的:

代码语言:javascript
复制
public class MainFragment extends Fragment {

    private MainFragmentBinding binding;
    private ToolbarBinding toolbarBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        binding = MainFragmentBinding.inflate(inflater, container, false);
        toolbarBinding = binding.toolbar;

        return binding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        toolbarBinding = null;
        binding = null;
    }
}

现在有两个绑定:一个对应于主布局,另一个对应于包含的布局。

票数 38
EN

Stack Overflow用户

发布于 2021-03-14 21:01:29

如果您想绑定包含的布局,

For Activity

代码语言:javascript
复制
YourMainLayoutBinding mainLayoutBinding = MainLayoutBinding.inflate(getLayoutInflater);

View view = mainLayoutBinding.getRoot();

YourIncludedLayoutBinding includedLayoutBinding = YourIncludedLayoutBinding.bind(View);

片段

代码语言:javascript
复制
YourMainLayoutBinding mainLayoutBinding = MainLayoutBinding.inflate(inflater,container,false);

View view = mainLayoutBinding.getRoot();

YourIncludedLayoutBinding includedLayoutBinding = YourIncludedLayoutBinding.bind(View);

确保如果主布局绑定父根是LinearLayout,那么includedLayoutBinding父布局也是线性布局

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58730127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档