如何将RecyclerView置于Android屏幕中央?

huangapple go评论60阅读模式
英文:

How can I put the Recyclerview at the Center of the screen in Android?

问题

我有一个RecyclerView,用来显示所有保存的卡片,但我的唯一问题是,它永远不会显示在屏幕中间,而是显示在屏幕的右侧。我甚至尝试了设置marginLeft,但问题仍然存在。下面是我的XML布局文件中的两个布局文件的代码。

fragment_to_learn.xml

<androidx.constraintlayout.widget.ConstraintLayout
    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_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@drawable/flash_tablebar"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:layout_alignParentTop="true"
        tools:ignore="MissingConstraints">

        <ImageView
            android:id="@+id/back_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ic_arrow_back" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        </LinearLayout>
    </androidx.appcompat.widget.Toolbar>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        tools:listitem="@layout/recycle_view"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

recycle_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_margin="20dp"
    android:layout_marginTop="40dp"
    android:background="@drawable/rounded_cardview"
    android:layout_width="match_parent"
    android:layout_height="400dp">

    <TextView
        android:layout_gravity="center"
        android:id="@+id/textView"
        style="@style/word_title"
        android:textColor="#FFFF"
        android:textStyle="bold"
        android:layout_marginStart="30dp"
        android:text="@string/word1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_marginLeft="20dp"
        android:layout_gravity="center"
        android:text="@string/word2"
        android:textColor="#FFFF"
        android:textStyle="bold"
        android:id="@+id/text1"
        style="@style/word_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageDelete"
        android:src="@drawable/baseline_delete_24"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp" />
</LinearLayout>

这是我的屏幕的实际样子,而不是将RecyclerView居中显示的样子。

英文:

I have a recycler view that displays all the saved cards but my only issue is , it never comes at the middle of the screen but to the right of the screen. I even tried marginleft but still the problem persists. Below is my code XML code of the both layout files present in it .

fragment_to_learn.xml

&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;androidx.appcompat.widget.Toolbar
        android:id=&quot;@+id/toolbar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;80dp&quot;
        android:background=&quot;@drawable/flash_tablebar&quot;
        android:elevation=&quot;4dp&quot;
        android:theme=&quot;@style/ThemeOverlay.AppCompat.ActionBar&quot;
        android:layout_alignParentTop=&quot;true&quot;
        tools:ignore=&quot;MissingConstraints&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/back_icon&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;10dp&quot;
            android:src=&quot;@drawable/ic_arrow_back&quot; /&gt;
        &lt;LinearLayout
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
        &lt;/LinearLayout&gt;
    &lt;/androidx.appcompat.widget.Toolbar&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerview&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:layout_marginLeft=&quot;20dp&quot;
        tools:listitem=&quot;@layout/recycle_view&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.5&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

recycle_view.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:orientation=&quot;horizontal&quot;
    android:layout_margin=&quot;20dp&quot;
    android:layout_marginTop=&quot;40dp&quot;
    android:background=&quot;@drawable/rounded_cardview&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;400dp&quot;&gt;

    &lt;TextView
        android:layout_gravity=&quot;center&quot;
        android:id=&quot;@+id/textView&quot;
        style=&quot;@style/word_title&quot;
        android:textColor=&quot;#FFFF&quot;
        android:textStyle=&quot;bold&quot;
        android:layout_marginStart=&quot;30dp&quot;
        android:text=&quot;@string/word1&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_weight=&quot;1&quot; /&gt;

    &lt;TextView
        android:layout_marginLeft=&quot;20dp&quot;
        android:layout_gravity=&quot;center&quot;
        android:text=&quot;@string/word2&quot;
        android:textColor=&quot;#FFFF&quot;
        android:textStyle=&quot;bold&quot;
        android:id=&quot;@+id/text1&quot;
        style=&quot;@style/word_title&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_weight=&quot;1&quot;/&gt;

    &lt;ImageView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/imageDelete&quot;
        android:src=&quot;@drawable/baseline_delete_24&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginLeft=&quot;10dp&quot;
        android:layout_marginRight=&quot;20dp&quot; /&gt;
&lt;/LinearLayout&gt;

This is how my screen looks like instead of having the recyclerview at the center. 如何将RecyclerView置于Android屏幕中央?

答案1

得分: 1

请从下面的代码中删除这一行 android:layout_marginLeft="20dp"。

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="20dp"
    tools:listitem="@layout/recycle_view"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5" />
英文:

Remove this line android:layout_marginLeft="20dp" from below code.

&lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerview&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:layout_marginLeft=&quot;20dp&quot; // Remove this line
        tools:listitem=&quot;@layout/recycle_view&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/toolbar&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.5&quot; /&gt;

答案2

得分: 0

为了将 RecyclerView 放置在屏幕中央,请按照以下方式将它放入一个嵌套的 LinearLayout 中:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:listitem="@layout/recycle_view" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

将这个 LinearLayout 放入你的 ConstraintLayout 中,与所有其他元素一起。

修改了 fragment_to_learn.xml 文件:

<androidx.constraintlayout.widget.ConstraintLayout
    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_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@drawable/flash_tablebar"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:layout_alignParentTop="true"
        tools:ignore="MissingConstraints">

        <ImageView
            android:id="@+id/back_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ic_arrow_back" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        </LinearLayout>
    </androidx.appcompat.widget.Toolbar>

    <!-- 添加嵌套的线性布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingBottom="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

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

                <!-- 将 android:layout_width 和 android:layout_height 从 0dp 更改为 wrap_content -->
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:listitem="@layout/recycle_view" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

你还需要将 android:layout_widthandroid:layout_height0dp 更改为 wrap_content

英文:

To position the RecyclerView in the center of the screen, put it in a nested LinearLayout like below:

&lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;
            android:paddingBottom=&quot;2dp&quot;&gt;

            &lt;LinearLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:orientation=&quot;horizontal&quot;&gt;

                &lt;LinearLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:orientation=&quot;horizontal&quot;
                    android:gravity=&quot;center&quot;&gt;

                    &lt;androidx.recyclerview.widget.RecyclerView
                        android:id=&quot;@+id/recyclerview&quot;
                        android:layout_width=&quot;wrap_content&quot;  
                        android:layout_height=&quot;wrap_content&quot;
                        tools:listitem=&quot;@layout/recycle_view&quot; /&gt;

                &lt;/LinearLayout&gt;

            &lt;/LinearLayout&gt;

        &lt;/LinearLayout&gt;

Put the LinearLayout inside your ConstraintLayout along with all the other elements.

Modified fragment_to_learn.xml file:

&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;androidx.appcompat.widget.Toolbar
        android:id=&quot;@+id/toolbar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;80dp&quot;
        android:background=&quot;@drawable/flash_tablebar&quot;
        android:elevation=&quot;4dp&quot;
        android:theme=&quot;@style/ThemeOverlay.AppCompat.ActionBar&quot;
        android:layout_alignParentTop=&quot;true&quot;
        tools:ignore=&quot;MissingConstraints&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/back_icon&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;10dp&quot;
            android:src=&quot;@drawable/ic_arrow_back&quot; /&gt;
        &lt;LinearLayout
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
        &lt;/LinearLayout&gt;
    &lt;/androidx.appcompat.widget.Toolbar&gt;

    &lt;!--Add nested linear layout--&gt; 
    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:orientation=&quot;horizontal&quot;
        android:paddingBottom=&quot;2dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:orientation=&quot;horizontal&quot;&gt;

            &lt;LinearLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:gravity=&quot;center&quot;
                android:orientation=&quot;horizontal&quot;&gt;
                
                &lt;!--Change from 0dp to wrap_content--&gt;
                &lt;androidx.recyclerview.widget.RecyclerView
                    android:id=&quot;@+id/recyclerview&quot;
                    android:layout_width=&quot;wrap_content&quot;  
                    android:layout_height=&quot;wrap_content&quot;
                    tools:listitem=&quot;@layout/recycle_view&quot; /&gt;

            &lt;/LinearLayout&gt;

        &lt;/LinearLayout&gt;

    &lt;/LinearLayout&gt;
    
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

You also need to change the android:layout_width and android:layout_height from 0dp to wrap_content.

huangapple
  • 本文由 发表于 2023年7月3日 19:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定