RecyclerView的Width超过DeviceWidth并破坏TableLayout

RecyclerView's Width exceeds DeviceWidth and destroys TableLayout

我在 Table(Row)Layout 中有一个 recylcerView,在 NestedScrollView 中,它们都将属性 layout_width 设置为 match_parent。我遇到的问题是 recyclerView 的滚动不起作用,但是当尝试完成滚动(使用 NestedScrollView)时,我发现这不是真正的问题。问题是我的水平 recylcerView 的宽度扩展了设备宽度。我只是弄明白了,因为我的 recyclerView 上面的行没有正确显示文本。

下图展示了RV和上面textRow的行为 screenshot

左边的图片:在这里你可以看到上面文本行中的文本丢失了"h word"
标有 2 的图像:这是预期的输出。

如果我将 recyclerview 的 layout_width 设置为“200dp”,我只能达到所需的 "correct" 大小。但我不想要固定大小(由于不同的设备)。

我尝试使用以下代码动态设置 RV 的 layout_width,但没有成功:

LinearLayoutManager llm = new LinearLayoutManager(this.getContext(), LinearLayoutManager.HORIZONTAL, false);
    mRecyclerViewAttachments.setLayoutManager(llm);
    ViewGroup.LayoutParams params = mRecyclerViewAttachments.getLayoutParams();
    int newWidth = (int) (params.width * 0.9);
    mRecyclerViewAttachments.setLayoutParams(params);

谁能帮我解决这个问题?下面是对话框的完整布局xml(RV终于来了):

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

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detail_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:animateLayoutChanges="true">

    <TableLayout
        android:id="@+id/tableLayoutDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">

        <!-- ROW 1 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_column="0"
                android:layout_span="2"
                android:text="@string/detail_fragment_entry_header_1"
                android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                tools:text="@string/detail_fragment_entry_header_1"
                android:background="@color/colorPrimary"/>

        </TableRow>

        <!-- ROW 2 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:src="@drawable/ic_event_black_24dp"
                android:layout_height="wrap_content"
                android:layout_width="48dp"
                android:layout_gravity="left"
                android:layout_column="0"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_column="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:textAppearance="@style/ChronlyTextViewHint"
                    android:text="@string/created_entry"
                    tools:text="@string/created_entry" />

                <TextView
                    android:id="@+id/textViewDate"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/detail_dlg_phone_choose"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="@string/detail_dlg_phone_choose" />
            </LinearLayout>
        </TableRow>

        <!-- ROW 3 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:id="@+id/imageViewEventType"
                android:src="@drawable/ic_contacts_black_24dp"
                android:layout_height="wrap_content"
                android:layout_width="48dp"
                android:layout_gravity="left"
                android:layout_column="0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_column="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:textAppearance="@style/ChronlyTextViewHint"
                    android:text="@string/detail_dlg_contact"
                    tools:text="@string/detail_dlg_contact" />

                <TextView
                    android:id="@+id/textViewContact"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/detail_dlg_contact_desc"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="@string/detail_dlg_contact_desc" />
            </LinearLayout>
        </TableRow>

        <!-- ROW 4 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorDarkGray"
                android:layout_column="0"
                android:layout_span="2">
            </LinearLayout>
        </TableRow>
        <!-- ROW 5 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="8dp"
            android:paddingTop="8dp">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_span="2">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/editTextComment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="top|left"
                    android:inputType="textMultiLine"
                    android:scrollbars="vertical|horizontal"
                    android:scrollHorizontally="false"
                    android:overScrollMode="always"
                    android:scrollbarStyle="insideInset"
                    android:hint="Enter a comment"
                    android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                    tools:text="first word, second word, third word, fourth word, fifth word, sixth word, seventh word" />

            </android.support.design.widget.TextInputLayout>
        </TableRow>
        <!-- ROW 6 -->
        <TableRow
            android:id="@+id/rowDetailLabelAttachment"
            android:layout_width="match_parent"
            android:paddingRight="16dp"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_gravity="left"
                android:layout_span="2"
                android:textAppearance="@style/ChronlyTextViewHint"
                android:text="@string/detail_label_attachment"
                tools:text="Attachments"/>
        </TableRow>
        <!-- ROW 7 -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp">

            <ImageView
                android:layout_width="48dp"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_attachment_black_24dp"
                android:layout_column="0"
                android:layout_gravity="left"/>
            <!--  -->

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewAttachments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:scrollbars="horizontal"
                android:overScrollMode="always"
                android:scrollbarStyle="insideInset"
                android:nestedScrollingEnabled="false"
                android:visibility="visible"></android.support.v7.widget.RecyclerView>

        </TableRow>
    </TableLayout>
</android.support.v4.widget.NestedScrollView>

你的树应该是这样的

<NestedScrollView>

  <LinearLayout  orientation = vertical>

     <EditText>

     <TextView>

     <Recyclerview>

  </LinearLayout>

</NestedScrollView>