【问题标题】:How to set up this row in XML?如何在 XML 中设置这一行?
【发布时间】:2016-08-22 07:42:36
【问题描述】:

我试图在DialogFragment 中显示RecyclerView,但我无法让行项XML 正常工作。

我正在尝试显示如下内容:

TEXTVIEW1 EDITTEXT TEXTVIEW2

其中TEXTVIEW1 占宽度的 1/2,EDITTEXT 默认占宽度的 1/4,TEXTVIEW2 占宽度的 1/4(其余部分)。

我将这三个Views 固定在一个水平的LinearLayout 中。我给每个View 一个layout_heightwrap_content 和一个layout_width0dp 然后我将layout_weight 值分别分配给2, 1, 1

但是它没有按预期显示。在我用数据和文本填充所有行之后,EditText 的宽度似乎换成了它们的值,而不是占据我指定的四分之一部分。

我该如何解决这个问题?

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="starttext"
        android:textSize="16sp"
        android:layout_weight="2"
        />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal"
        android:text="14.0"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="endingtext"
        android:layout_weight="1"
        />

</LinearLayout>

我的适配器:

public class SpecialobjectHistoryRecyclerViewAdapter extends RecyclerView.Adapter<SpecialobjectHistoryRecyclerViewAdapter.RecyclerViewHolder> {
    public static final String TAG = SpecialobjectRecyclerViewAdapter.class.getSimpleName();
    private List<SpecialobjectHistory> mSpecialobjectHistoryList;
    private Context mContext;

    public SpecialobjectHistoryRecyclerViewAdapter(Context context, List<SpecialobjectHistory> specialobjectHistoryList) {
        mContext = context;
        mSpecialobjectHistoryList = specialobjectHistoryList;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_specialobject_history_row, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.bindRow(mSpecialobjectHistoryList.get(position));
    }

    @Override
    public int getItemCount() {
        return mSpecialobjectHistoryList.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public SpecialobjectHistory thisSpecialobjectHistory;
        public TextView specialobjectTimestampTextView;
        public EditText specialobjectValueEditText;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            specialobjectTimestampTextView = (TextView) itemView.findViewById(R.id.item_specialobject_history_row_textview_timestamp);
            specialobjectValueEditText = (EditText) itemView.findViewById(R.id.item_specialobject_history_row_edittext_value);
        }

        public void bindRow(final SpecialobjectHistory specialobjectHistory) {
            thisSpecialobjectHistory = specialobjectHistory;
            specialobjectTimestampTextView.setText(specialobjectHistory.getTimestamp());
            specialobjectValueEditText.setText(specialobjectHistory.getValue() + "");
        }

    }
}

【问题讨论】:

  • 你能分享你的xml代码吗?
  • 水平布局所有项目匹配父项和所有项目权重1
  • 在高度和宽度上匹配父级? (编辑:使用匹配父级肯定不起作用)
  • @MohamedIbrahim 添加了 XML

标签: android xml layout android-edittext android-recyclerview


【解决方案1】:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:text="starttext"
    android:textSize="16sp"
    android:layout_weight="2" />

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="numberDecimal" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:text="endingtext"
    android:layout_weight="1" />

</LinearLayout>

未经测试,但应该可以工作

【讨论】:

  • 这和我的有什么不同?
  • android:layout_width="0dp"
【解决方案2】:

你应该将第一个 Textview 的宽度设置为 0dp

android:layout_width="0dp"

【讨论】:

  • 当我第一次复制时,您的 cod 是 match_parent,但请检查您的代码并试一试
  • 我正在使用的代码使用 0dp 并且它不起作用。间距仍然完全关闭。
  • 那么我们应该会看到您的 RecyclerView 适配器。可能是程序问题。
  • 我对此表示怀疑——我所做的只是使用.setText()设置每个字段
  • 您是否也为 EditText 这样做了?
猜你喜欢
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
相关资源
最近更新 更多