【问题标题】:Align text in RecyclerView with titles将 RecyclerView 中的文本与标题对齐
【发布时间】:2020-06-21 10:10:53
【问题描述】:

您好,我制作了一个带有订单的 RecyclerView。但是弹出窗口中的列表与标题不一致。因为图片胜于雄辩,在这里你可以看到:

但我需要的是这样的:

我怎样才能做到这一点?我怎样才能很好地定义列,以便每篇文章都很好地适合标题?

这是我的 RecyclerView:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">



        <TextView
            android:id="@+id/order_overview_artikel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Artikel"
            android:layout_marginRight="10dp"
            android:textSize="16dp"/>

        <TextView
            android:id="@+id/order_overview_anzahl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Anzahl"
            android:layout_marginRight="10dp"
            android:textSize="16dp"/>

        <TextView
            android:id="@+id/order_overview_preis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Preis"
            android:textSize="16dp"/>



</LinearLayout>

这是弹出窗口:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:gravity="center">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="25dp"
                android:orientation="vertical">


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

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:text="Bestellübersicht"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:textStyle="bold"></TextView>

                    <TextView
                        android:id="@+id/order_overview_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:text="0"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="10dp"
                        android:text="Produkt"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">

                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:text="Anzahl"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">

                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:text="Preis"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">

                    </TextView>

                </LinearLayout>

                <androidx.coordinatorlayout.widget.CoordinatorLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <androidx.core.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="true"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

                        </androidx.recyclerview.widget.RecyclerView>
                    </androidx.core.widget.NestedScrollView>


                </androidx.coordinatorlayout.widget.CoordinatorLayout>

                <com.airbnb.lottie.LottieAnimationView
                    android:layout_width="match_parent"
                    android:layout_height="128dp"
                    app:lottie_autoPlay="true"
                    app:lottie_loop="true"
                    app:lottie_rawRes="@raw/walkingburger" />

                <Button
                    android:id="@+id/btn_order_overview_finish"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/button_order_checkout"
                    android:backgroundTint="#9BC3BF"
                    android:elevation="16dp"
                    android:text="FERTIG"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"
                    app:layout_constraintVertical_bias="0.777" />


            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</RelativeLayout>

【问题讨论】:

    标签: java android android-studio android-recyclerview


    【解决方案1】:

    您需要对标题布局和回收站视图项使用类似的布局模式。这是

    使用

    android:weightSum="3"
    

    对于子视图,将 layout_weight 设置为 1。所有三个都将对齐。 我已对您的代码进行了更改。

    RecyclerView 项目

    <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_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="center">
    
    
    
        <TextView
            android:id="@+id/order_overview_artikel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Artikel"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginRight="10dp"
            android:textSize="16dp"/>
    
        <TextView
            android:id="@+id/order_overview_anzahl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Anzahl"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginRight="10dp"
            android:textSize="16dp"/>
    
        <TextView
            android:id="@+id/order_overview_preis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Preis"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="16dp"/>
    
    
    
    </LinearLayout>
    

    你的弹出窗口

    <?xml version="1.0" encoding="utf-8"?>
    <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:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:gravity="center">
    
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="15dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="25dp"
                android:orientation="vertical">
    
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:orientation="horizontal">
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:text="Bestellübersicht"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:textStyle="bold"></TextView>
    
                    <TextView
                        android:id="@+id/order_overview_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:text="0"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:textStyle="bold" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:weightSum="3"
                    android:orientation="horizontal">
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="10dp"
                        android:text="Produkt"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">
    
                    </TextView>
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:text="Anzahl"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">
    
                    </TextView>
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:text="Preis"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textAlignment="center"
                        android:textSize="18dp"
                        android:textStyle="bold">
    
                    </TextView>
    
                </LinearLayout>
    
                <androidx.coordinatorlayout.widget.CoordinatorLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <androidx.core.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="true"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/recyclerview_order_scroll"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
    
                        </androidx.recyclerview.widget.RecyclerView>
                    </androidx.core.widget.NestedScrollView>
    
    
                </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
                <com.airbnb.lottie.LottieAnimationView
                    android:layout_width="match_parent"
                    android:layout_height="128dp"
                    app:lottie_autoPlay="true"
                    app:lottie_loop="true"
                    app:lottie_rawRes="@raw/walkingburger" />
    
                <Button
                    android:id="@+id/btn_order_overview_finish"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/button_order_checkout"
                    android:backgroundTint="#9BC3BF"
                    android:elevation="16dp"
                    android:text="FERTIG"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"
                    app:layout_constraintVertical_bias="0.777" />
    
    
            </LinearLayout>
    
        </LinearLayout>
    
    </androidx.cardview.widget.CardView>
    
    
    </RelativeLayout>
    

    【讨论】:

    • 非常感谢! :) 我希望你是最好的。很多钱和全世界的健康! :)
    • 编码愉快!我希望你也一样。 :)
    【解决方案2】:

    首先,在您的弹出窗口中,对于“Produkt”、“Anzahl”等,您可以像“match_parent”一样编写 layout_width,但在您的 RecyclerView 中,项目的 layout_width 是 wrap_content。在 RecyclerView 中更改 match_parent 上的 layout_width。

    【讨论】:

      【解决方案3】:

      你需要一个表格视图布局来对齐文本,你也可以使用线性布局,但它棘手和复杂的 UI 解决方案会在 UI 层上执行相当繁重的操作来呈现(例如在线性布局中使用权重)。 查看这个android官方link了解更多。

      【讨论】:

      • 如何在recyclerview的上下文中使用它?
      猜你喜欢
      • 2021-01-03
      • 2019-01-07
      • 2011-03-07
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 2021-11-09
      • 2012-08-25
      • 2015-06-10
      相关资源
      最近更新 更多