【问题标题】:Android EditText match_parent not working in recycler viewAndroid EditText match_parent 在回收站视图中不起作用
【发布时间】:2015-11-28 09:26:21
【问题描述】:

我有一个 activity,里面有 viewpager 和其中一个 fragment 我正在使用 Recycler 视图 将查看项目作为编辑文本。这是我的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFF00"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="@dimen/five">

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="top"
        android:padding="@dimen/three" />

    <EditText
        android:id="@+id/etEditorListItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="@string/et_add_item"
        android:padding="@dimen/three"/>
</LinearLayout>

在这里,我将 edittext 宽度指定为 match_parent ( android:layout_width="match_parent") 但在运行时它会根据提示的宽度进行包装。 有人可以帮我解决这个问题吗?


适配器:

public class ListAdapter extends RecyclerView.Adapter<NoteListAdapter.ListHolder> {
private List<String> mList = new ArrayList<String>();
private Context mContext;

public ListAdapter(List<String> list){
    this.mList = list;
}

@Override
public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    this.mContext = parent.getContext();
    return new ListHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,null));
}

@Override
public void onBindViewHolder(final ListHolder holder, final int position) {
    holder.etEditorListItem.setText(mList.get(position));

}


@Override
public int getItemCount() {
    return mList != null ? mList.size() : 0;
}

public class ListHolder extends RecyclerView.ViewHolder{

    public CheckBox checkBox;
    public EditText etEditorListItem;

    public ListHolder(View itemView) {
        super(itemView);
        checkBox = (CheckBox)itemView.findViewById(R.id.checkbox);
        etEditorListItem = (EditText)itemView.findViewById(R.id.etEditorListItem);
    }
}
}


片段:

RecyclerView rvList = (RecyclerView) view.findViewById(R.id.rvList);
rvList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvList.setLayoutManager(layoutManager);
mListAdapter = new ListAdapter(listItems);
rvList.setAdapter(mListAdapter);

片段布局:

<android.support.v7.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

【问题讨论】:

  • 对 EditText 和 CheckBox 使用权重。
  • 试过了也不行。
  • 你检查过LinearLayout的宽度吗,试着给它设置背景看看它占据的宽度。
  • 是的,我已经为 LinearLayout 添加了背景颜色:android:background="#FFFF00" 它的行为方式也与它的宽度被包裹的方式相同。认为这是因为编辑文本的宽度被包裹到它的提示宽度。
  • 同时发布包含RecyclerView 的片段xml。

标签: android android-edittext android-recyclerview


【解决方案1】:

试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"  
android:gravity="center_vertical"
android:orientation="horizontal"
 android:background="#FFFF00"
android:weightSum="1"
android:padding="5dp">

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.1"
    android:padding="3dp"/>

<EditText
    android:id="@+id/etEditorListItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@null"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.9"       
    android:hint="Hello"
    android:padding="3dp"/>

【讨论】:

  • 不起作用。此布局代码是正确的,并且当您直接在视图中使用它时可以正常工作。但是在使用 RecyclerView 时,相同的代码不起作用。我想补充一点,我正在使用视图寻呼机和内部视图寻呼机回收器视图位于片段内。所以它的 Activity > ViewPager > Fragment > RecyclerView > View Item
猜你喜欢
  • 1970-01-01
  • 2021-04-16
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 2019-08-16
相关资源
最近更新 更多