如何用XML或Kotlin中的代码固定RecyclerView的高度

时间:2019-12-23 11:46:03

标签: android android-recyclerview kotlin-android-extensions

我尝试了下面的代码,但是一直以来,它为我提供了全屏的recyclerview,如何解决这个问题。

请注意,我的recyclerview中只有两行/每个项目

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:tools="http://schemas.android.com/tools">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            tools:listitem="@layout/recyclerview_item" />

    </RelativeLayout>

    <EditText
        android:id="@+id/textfield"
        android:layout_width="294dp"
        android:layout_height="53dp"
        android:layout_marginEnd="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginBottom="164dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent">

    </EditText>

</androidx.constraintlayout.widget.ConstraintLayout>

4 个答案:

答案 0 :(得分:2)

Yes you can do it with constraint layout here is the code you can follow 

注意:请执行cleanRebuild Project以反映我在Android Studio 3.5+中注意到的正确的UI更改,有时UI不能反映出来。

    

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        tools:listitem="@layout/recyclerview_item"
        app:layout_constraintBottom_toTopOf="@id/textfield"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <EditText
        android:id="@+id/textfield"
        android:layout_width="196dp"
        android:layout_height="56dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="164dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recyclerview1" />


</androidx.constraintlayout.widget.ConstraintLayout>

答案 1 :(得分:0)

尝试以下布局:

    static void Main(string[] args)
    {
        string input = "input";
        AddString(ref input);
        System.Console.WriteLine(input);
    }

    private static void AddString(ref string input)
    {
        input += "_edited";
    }

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" xmlns:tools="http://schemas.android.com/tools"> <RelativeLayout android:id="@+id/recyclerview_layout" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview1" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listitem="@layout/recyclerview_item" /> </RelativeLayout> <EditText android:id="@+id/textfield" android:layout_width="294dp" android:layout_height="53dp" android:layout_marginEnd="80dp" android:layout_marginRight="80dp" app:layout_constraintTop_toBottomOf="@+id/recyclerview_layout" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> </EditText> </androidx.constraintlayout.widget.ConstraintLayout> 应该将recyclerview_item用作wrap_content

layout_height

输出: enter image description here

您还可以通过使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!--Should be wrap_content--> <!--Here list item content--> </LinearLayout> 来实现此目的,如下所示:

LinearLayout

答案 2 :(得分:0)

使用relativelayout实现此目的的最简单方法。 只需将您的代码替换为此。

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/recyclerview_item"
        android:layout_above="@id/textfield"//Recyclerview will take above area.
       />
     <EditText
        android:id="@+id/textfield"
        android:layout_width="294dp"
        android:layout_height="53dp"
        android:layout_marginEnd="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginBottom="164dp"
        android:layout_alignParentBottom="true"
       />

  </RelativeLayout>

答案 3 :(得分:0)

以下是正确执行操作的方法:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        tools:listitem="@layout/recyclerview_item"
        app:layout_constraintBottom_toTopOf="@id/textfield"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <EditText
        android:id="@+id/textfield"
        android:layout_width="294dp"
        android:layout_height="53dp"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="80dp"
        android:layout_marginBottom="164dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recyclerview1" />

</androidx.constraintlayout.widget.ConstraintLayout>

我希望这是您想要的。