【问题标题】:RecyclerView's Width exceeds DeviceWidth and destroys TableLayoutRecyclerView的Width超过DeviceWidth并破坏TableLayout
【发布时间】:2017-11-15 19:24:15
【问题描述】:

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

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

左图:这里可以看到上面文本行中的文本缺少“h word”
标有 2 的图像:这是预期的输出。

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

我尝试使用以下代码动态设置 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"/>
            <!-- https://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working/37338715#37338715 -->

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

【问题讨论】:

  • 你想让布局像屏幕截图一样吗?
  • 是的,就像右边的一样
  • 那你为什么要把这个弄得太复杂?
  • 你有什么建议?

标签: android android-layout android-recyclerview android-tablelayout


【解决方案1】:

你的树应该是这样的

<NestedScrollView>

  <LinearLayout  orientation = vertical>

     <EditText>

     <TextView>

     <Recyclerview>

  </LinearLayout>

</NestedScrollView>

【讨论】:

  • 我认为 TableLayout 是一个不错的选择,我会试试你的解决方案
  • 非常感谢,一切正常。太容易想到了,因为我想要一个好的和干净的设计。我错了
猜你喜欢
  • 2019-03-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多