如何使 CardView 和 CardView 中的 RecyclerView 具有低项目时的大小
How to make RecyclerView inside CardView and CardView have it's size when it has low items
我在 CardView
里面有一个 RecyclerView
,但是我有一个问题,我在 RecyclerView
底部有 2 个按钮,所以我把它们放在 RelativeLayout
的末尾] 并给它们 parent_bottom
true 这样我就可以让它们可见但是这使得 CardView
有全屏无论 RecyclerView
是否有项目。
Xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btnClear"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:id="@+id/btnClear"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnContinue"
android:layout_alignParentBottom="true"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</RelativeLayout>
</android.support.v7.widget.CardView>
当我尝试另一件事时,下面的代码使它们低于 RecyclerView
所以它修复了情况和其他情况,如图所示(底部的按钮未显示)
那么如何解决这个问题呢?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</RelativeLayout>
</android.support.v7.widget.CardView>
目标是这个,当项目展开时它只适合屏幕并且不会超出它
尝试将 CardView
内的 RelativeLayout
更改为垂直方向的 LinearLayout
。
请使用以下代码。它运行完美。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColor="@color/blue_normal"
android:background="?selectableItemBackground"
android:text="Continue" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
首先,您可以通过将 CardView
中的 relativelayout
更改为 LinearLayout
并为按钮创建嵌套的相对布局来解决。为了避免嵌套布局尝试我下面的代码将帮助你..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="25dp"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnClear"
android:layout_gravity="bottom"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnContinue"
android:layout_gravity="bottom|end"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/bb_darkBackgroundColor" />
</android.support.v7.widget.CardView>
</LinearLayout>
Adjust recylerview top and bottom margin according to your needs.
只需将 Recyclerview 权重设置为 1,这意味着父级应该是 LinearLayout
请检查下面的代码
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text=" Your active inspections"
android:textSize="18sp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
/>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>
我在 CardView
里面有一个 RecyclerView
,但是我有一个问题,我在 RecyclerView
底部有 2 个按钮,所以我把它们放在 RelativeLayout
的末尾] 并给它们 parent_bottom
true 这样我就可以让它们可见但是这使得 CardView
有全屏无论 RecyclerView
是否有项目。
Xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btnClear"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:id="@+id/btnClear"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnContinue"
android:layout_alignParentBottom="true"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</RelativeLayout>
</android.support.v7.widget.CardView>
当我尝试另一件事时,下面的代码使它们低于 RecyclerView
所以它修复了情况和其他情况,如图所示(底部的按钮未显示)
那么如何解决这个问题呢?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</RelativeLayout>
</android.support.v7.widget.CardView>
目标是这个,当项目展开时它只适合屏幕并且不会超出它
尝试将 CardView
内的 RelativeLayout
更改为垂直方向的 LinearLayout
。
请使用以下代码。它运行完美。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Your active inspections"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rvActiveInspections"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColor="@color/blue_normal"
android:background="?selectableItemBackground"
android:text="Continue" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout"
android:layout_below="@+id/activeInspections"
android:layout_marginTop="8dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
首先,您可以通过将 CardView
中的 relativelayout
更改为 LinearLayout
并为按钮创建嵌套的相对布局来解决。为了避免嵌套布局尝试我下面的代码将帮助你..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="25dp"
android:text=" Your active inspections"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnClear"
android:layout_gravity="bottom"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:id="@+id/btnContinue"
android:layout_gravity="bottom|end"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/bb_darkBackgroundColor" />
</android.support.v7.widget.CardView>
</LinearLayout>
Adjust recylerview top and bottom margin according to your needs.
只需将 Recyclerview 权重设置为 1,这意味着父级应该是 LinearLayout 请检查下面的代码
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
app:cardElevation="8dp">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/activeInspections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text=" Your active inspections"
android:textSize="18sp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvActiveInspections"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
/>
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:background="?selectableItemBackground"
android:text="CLear" />
<Button
android:id="@+id/btnContinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:background="?selectableItemBackground"
android:text="Continue"
android:textColor="@color/blue_normal" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>