在布局膨胀时,查看不会显示

时间:2017-02-01 22:02:47

标签: android android-layout android-view

这是我的list_section.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="@color/colorBackgroundDark"
android:paddingBottom="10dp"
android:paddingTop="10dp">

<TextView
    android:id="@+id/textViewAssessmentTypeName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:paddingLeft="16dp"
    android:textColor="@color/colorPrimaryText" />

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#8000FF00" />
</RelativeLayout>

我通过以下方式在我的RecyclerViewAdapter中充气:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ...
    else if (viewType == TYPE_SECTION) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_section, parent, false);
        return new ViewHolderSection(view, callback);
    } 
    ...
}

private class ViewHolderSection extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    ItemClickListener callback;
    TextView assessmentTypeName;

    ViewHolderSection(View view, ItemClickListener callback) {
        super(view);
        this.callback = callback;
        assessmentTypeName = (TextView) view.findViewById(R.id.textViewAssessmentTypeName);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (callback != null) {
            callback.onItemClick(getAdapterPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (callback != null) {
            callback.onItemLongClick(getAdapterPosition());
        }
        return true;
    }
}

但由于某种原因,我在layout.xml中使用View作为相对于RelativeLayout的彩色滤镜/叠加层的视图无法显示。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我通过将list_section.xml更改为:

来修复它
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_comment"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/posted_comments"
    android:layout_above="@+id/write_comment">

</android.support.v7.widget.RecyclerView>


<LinearLayout
    android:id="@+id/write_comment"
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true">

    <EditText
        android:paddingEnd="10dp"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:background="@mipmap/ic_launcher"
        android:layout_height="match_parent"
        android:inputType="textPersonName|textMultiLine"
        android:text=""
        android:gravity="left|center"
        android:hint="Comment..."
        android:textSize="20sp"
        android:maxLength="100"
        android:id="@+id/current_comment" />

    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="@mipmap/ic_launcher"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:id="@+id/submit"
        android:layout_toEndOf="@+id/current_comment"
        />


</LinearLayout>
</RelativeLayout>

前一个没有用,因为RelativeLayout的layout_height最终为0。