【问题标题】:RecyclerView in dialog takes up (almost) whole area对话框中的 RecyclerView 占据(几乎)整个区域
【发布时间】:2017-09-19 07:02:26
【问题描述】:

我有一个布局对话框

subject_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">

    <TextView
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:text="Subject"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/subject_name"/>

    <LinearLayout
        android:weightSum="2"
        android:gravity="end"
        android:layout_below="@id/subject_name"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ViewSwitcher
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            android:layout_below="@id/subject_name"
            android:id="@+id/switcher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/please_wait"/>
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="10dp"
                android:id="@+id/studentRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </ViewSwitcher>

        <Button
            android:layout_weight="1"
            android:layout_below="@id/switcher"
            android:layout_alignParentRight="true"
            android:id="@+id/ok_Btn"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:background="@drawable/blue_button"
            android:text="ok"
            android:textColor="#fff"/>
    </LinearLayout>

</RelativeLayout>

创建对话框后,我正在从服务器获取数据以填充 RecyclerView。那时 ViewPager 的第一个孩子(带有进度条)是可见的。当我从服务器获取数据时,我将 RecyclerView 设置为可见子项。

这里的问题是,当 RecyclerView 有很多行时,ViewSwitcher 下包含 RecyclerView 的按钮只有部分可见(比如,三分之一的按钮不可见)。

屏幕截图(不应该是这样的)

但是当 recyclerView 没有那么多行时,它会按预期显示,

屏幕截图(它的外观)

我该如何解决这个问题?

【问题讨论】:

  • 如果您的姓名列表通常很长,那么 BottomSheet 可能比 Dialog developer.android.com/reference/android/support/design/widget/… 更合适
  • 30dp 中删除身高和体重,并将其设为 wrap_content
  • @sushildlh 按钮的重量?
  • 是的......
  • @Kuffs 我已经在同一个活动中使用过底页一次(看看第二个屏幕截图)。我认为从该底片创建另一个底片是不可取的

标签: android android-layout android-recyclerview


【解决方案1】:

当您为线性布局添加权重时,请记住:

将 android:weightSum="1" 设为一个,子布局权重从 0.0 到 1.0 例如:android:layout_weight="0.3"

使用布局权重作为 0dp 而不是 pf 硬编码值 例如:android:layout_height="0dp" 而不是 android:layout_height="30dp"

试试这个布局。希望它有效

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">

<TextView
    android:id="@+id/subject_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textColor="@color/black"
    android:textSize="18sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/subject_name"
    android:gravity="end"
    android:orientation="vertical"
    android:weightSum="1">

    <ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/subject_name"
        android:layout_marginTop="8dp"
        android:layout_weight="0.9">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:text="@string/please_wait" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"></android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:id="@+id/ok_Btn"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/switcher"
        android:layout_weight="0.1"
        android:background="@drawable/blue_button"
        android:text="ok"
        android:textColor="#fff" />
</LinearLayout>

【讨论】:

  • 有什么办法可以保持按钮大小固定为 30dp?
  • 我按照你发布的那样做了,但是按钮有点大,所以我将按钮大小固定为 30dp,它可以工作。但这不是一个肮脏的修复吗?我是否通过设置固定大小来覆盖重量方面?
  • 我的权重概念是为每个不同屏幕上的视图提供特定的高度或宽度。也许你应该尝试减少按钮重量并检查。
  • 像这样尝试..从Linearlayout中删除weightsum,将layout_weight添加到ViewSwitcher为1,然后将按钮高度添加到30dp。如果您觉得我的回答令人满意,请接受。
【解决方案2】:

您应该在布局中正确维护您的 weightSumweight。有关更多知识,您可以查看this solution 并像这样申请

  <LinearLayout
        android:weightSum="2"
        android:gravity="end"
        android:layout_below="@id/subject_name"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ViewSwitcher
            android:layout_weight="1.5"
            android:layout_marginTop="8dp"
            android:layout_below="@id/subject_name"
            android:id="@+id/switcher"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <LinearLayout
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/please_wait"/>
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="10dp"
                android:id="@+id/studentRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </ViewSwitcher>

        <Button
            android:layout_weight="0.5"
            android:layout_below="@id/switcher"
            android:layout_alignParentRight="true"
            android:id="@+id/ok_Btn"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@drawable/blue_button"
            android:text="ok"
            android:textColor="#fff"/>
    </LinearLayout>

【讨论】:

  • 你刚刚去掉了重量吗?
  • 不,我没有删除权重,但我更改了 LinearLayout 和 Button 中的权重值,同时在两者中设置了高度“0dp”。
  • 让我试试
  • 当然,你也可以根据自己的需要修改重量的数值。
  • 这在第一种情况下还可以(有很多行)。但是对于第二种情况(第二张截图)按钮太大了。
【解决方案3】:

在回收视图中使用 android:layout_marginBottom="30dp"

  <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:layout_marginBottom="30dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

【讨论】:

  • 没有变化。还是一样
  • 也在确定按钮中添加相同的内容
  • 添加 layout_marginBottom="30dp" 成功了,但它搞砸了另一种情况(RecyclerView 的行数更少),现在看起来很奇怪。
  • 只使用 ok 按钮高度 android:layout_height="wrap_content"
【解决方案4】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">

<TextView
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textSize="18sp"
    android:textColor="@color/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subject_name"/>

<RelativeLayout
    android:layout_below="@id/subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewSwitcher
        android:layout_marginTop="8dp"
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:paddingLeft="8dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/please_wait"/>
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:layout_below="@id/switcher"
        android:layout_alignParentRight="true"
        android:id="@+id/ok_Btn"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="@drawable/blue_button"
        android:text="ok"
        android:textColor="#fff"/>
</RelativeLayout>

【讨论】:

  • 我以前试过这样的东西。现在按钮不可见。所以我添加了带权重的线性布局,使其有点可见
【解决方案5】:

好的,现在检查!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">

<TextView
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textSize="18sp"
    android:textColor="@color/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subject_name"/>

<RelativeLayout
    android:layout_below="@id/subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewSwitcher
        android:layout_marginTop="8dp"
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:paddingLeft="8dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Please wait"/>
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:layout_weight="1"
        android:layout_alignParentRight="true"
        android:id="@+id/ok_Btn"
        android:layout_below="@id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="#369"
        android:text="OK"
        android:textColor="#fff"/>

</RelativeLayout>

</RelativeLayout>

【讨论】:

  • 第二张截图看起来很奇怪(只有两行)。
  • 尽管RelativeLayout(你替换linearLayout的那个)的高度是wrap_content,对于第二种情况,它会填满整个屏幕。
  • 您是否尝试执行我的代码?我改变了一些东西
  • 现在按钮在第一种情况下不可见Screenshot
猜你喜欢
  • 2019-08-10
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多