如何在XML中设置此行?

时间:2016-04-27 19:44:10

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

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

我试图显示这样的内容:

TEXTVIEW1 EDITTEXT TEXTVIEW2

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

我将这三个Views卡在水平LinearLayout内。我给View layout_height wrap_content layout_width 0dplayout_weight 2, 1, 1分别EditText分配了<?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() + ""); } } }

但是它没有按预期显示。在我用数据和文本填充所有行之后,<div class="row" style="width: 100%"> <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6"> <img src="https://heavyeditorial.files.wordpress.com/2016/02/david-joseph.jpg?quality=65&strip=all&strip=all/200x200" class="img-thumbnail img-responsive" alt="David Joseph"> <p class="caption">David Joseph was unarmed when he was fatally shot by Austin police. <em>Photo credit: APD</em></p> </div> <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6"> <img src="http://assets.nydailynews.com/polopoly_fs/1.2523996.1454945162!/img/httpImage/image.jpg_gen/derivatives/article_400/observer9n-3-web.jpg" class="img-thumbnail img-responsive" alt="Antronie Scott"> <p class="caption">San Antonio police fatally shot Antronie Scott, who was also unarmed. <em>Photo credit: SAPD</em></p></div> </div> 宽度似乎换算到它们的值而不是占用我指定的四分之一部分。

我该如何解决这个问题?

XML

{{1}}

我的适配器:

{{1}}

2 个答案:

答案 0 :(得分:0)

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

未经测试但应该正常工作

答案 1 :(得分:0)

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

android:layout_width="0dp"