首页 > 解决方案 > Android Java片段半屏显示

问题描述

我正在开发 android 应用程序。在fragment_auction_products.xml 中的recycler-view 中显示卡片列表时出现问题。当我导航到 FragmentAuctionProducts 时,我遇到 了问题

如您所见,我的列表在 50% 空间为空后开始显示......我该如何解决这个问题?

我的 fragment_auction_products.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/home_auction_products_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:gravity="top"
    android:layout_marginTop="10dp"/>

<ProgressBar
    android:id="@+id/home_auction_products_loader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:indeterminateDrawable="@drawable/spinner_ring"
    android:indeterminateOnly="true" />

</LinearLayout>

我的回收站查看项目:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="10dp"
        android:gravity="center"
        android:background="@drawable/layer_shadow"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/home_auction_products_recycler_view_image"
            android:layout_margin="4dp"
            android:layout_width="125dp"
            android:layout_height="150dp"
            android:src="@drawable/card_example_1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="10dp"
            android:orientation="vertical"
            android:paddingTop="15dp">

            <TextView
                android:id="@+id/home_auction_products_recycler_view_title"
                style="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/muli_bold"
                android:text="Title"
                android:textColor="@color/colorBlack"
                android:textSize="14dp" />

            <TextView
                android:id="@+id/home_auction_products_recycler_view_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/muli_bold"
                android:text="id"
                android:textColor="@color/colorBlack"
                android:textSize="0dp" />

            <TextView
                android:id="@+id/home_auction_products_recycler_view_current_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/muli_bold"
                android:text="50000 тг"
                android:textColor="@color/colorTextYellow"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/home_auction_products_recycler_view_start_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/muli"
                android:text="start-price"
                android:textColor="@color/colorBlack"
                android:textSize="12dp" />

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

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="8dp"
                    android:background="@drawable/auction_products_shape"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingLeft="4dp"
                    android:paddingTop="2dp"
                    android:paddingRight="4dp"
                    android:paddingBottom="2dp">

                    <TextView
                        android:id="@+id/home_auction_products_recycler_view_day"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/muli"
                        android:gravity="center"
                        android:text="10"
                        android:textColor="@color/colorWhite"
                        android:textSize="14dp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/muli"
                        android:gravity="center"
                        android:text="ДНЕЙ"
                        android:textColor="@color/colorWhite"
                        android:textSize="6dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@drawable/auction_products_shape"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingLeft="4dp"
                    android:paddingTop="2dp"
                    android:paddingRight="4dp"
                    android:paddingBottom="2dp">

                    <TextView
                        android:id="@+id/home_auction_products_recycler_view_hour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/muli"
                        android:gravity="center"
                        android:text="12"
                        android:textColor="@color/colorWhite"
                        android:textSize="14dp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/muli"
                        android:gravity="center"
                        android:text="ЧАСОВ"
                        android:textColor="@color/colorWhite"
                        android:textSize="6dp" />

                </LinearLayout>

            </LinearLayout>

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

                <TextView
                    android:id="@+id/home_auction_products_recycler_view_show"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_mdi_keyboard_arrow_right"
                    android:fontFamily="@font/muli"
                    android:textColor="@color/colorTextGray"
                    android:textSize="12dp" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

我称之为片段的主要容器:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context=".activity.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/bottom_nav_bar">

        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:windowBackground"
        app:menu="@menu/menu" />
</RelativeLayout>

标签: javaandroid

解决方案


只需使用android:layout_alignParentTop.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/home_auction_products_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:gravity="top"
    android:layout_marginTop="10dp"/>

<ProgressBar
    android:id="@+id/home_auction_products_loader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:indeterminateDrawable="@drawable/spinner_ring"
    android:indeterminateOnly="true" />

</LinearLayout>

希望这可以帮助。


推荐阅读