如何动态获取EditText的宽度并将其设置为recyclerView的宽度

2 人关注

我有一个编辑文本,它的宽度被设置为wrap-content.我也有一个回收器视图,它的宽度被设置为wrap-content.我想通过编程使回收器视图的宽度等于编辑文本的宽度。我使用了以下代码,但它没有工作。从getMeasuredWidth()返回的宽度不等于编辑文本的宽度。

    editText= findViewById(R.id.edit_text);
    editText.measure(0, 0);
    testRecyclerView= findViewById(R.id.employee_recycler_view);
    testRecyclerView.setLayoutManager(new LinearLayoutManager(context));
    ViewGroup.LayoutParams layoutParams =  testRecyclerView.getLayoutParams();
    layoutParams.width =  editText.getMeasuredWidth();