使用 TextViews 动态填充的 ScrollView 超过 "bounds" 个视图

ScrollView populated dynamically with TextViews is exceeding "bounds" of view

我正在尝试构建具有以下结构的布局:

Header 图片

滚动视图(使用文本视图动态填充)

静态菜单栏

<?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:id="@+id/root"
    tools:context=".ProfileActivity"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.071"
        tools:srcCompat="@tools:sample/avatars" />



        <ScrollView
            android:id="@+id/scrolly"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/ll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                ></LinearLayout>

        </ScrollView>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:layout_alignBottom="@+id/profile_pic"
        android:layout_gravity="bottom"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="-608dp"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_pic"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/menu_nav" />


</RelativeLayout>

我正在动态创建文本视图并将它们插入到滚动视图中的线性布局中,但它最终会在图像视图被填充后覆盖它,并且我的静态菜单栏无处可寻。

我不确定什么会导致这种行为 在这种情况下填充我的 ScrollView 的最佳做法是什么?

代码如下:

//create and display textviews of current inventory
private fun displayInventory(){

    var ll : LinearLayout = findViewById(R.id.ll)

    userInventory.forEach { element ->
        var uId = element.Uid.toString()
        var name = element.name.toString()
        var purchDate = element.purchDate.toString()
        var country = element.country.toString()
        var local = element.local.toString()
        var mine = element.mine.toString()
        var weight = element.weight.toString()
        var paid = element.paid.toString()
        var asking = element.asking.toString()
        var description = element.description.toString()
        var dimensions = element.dimensions.toString()
        var filePaths: ArrayList<String> = element.filePaths
        var downloadUrls: ArrayList<String> = element.downloadUrls

        val txtView = TextView(this)
        txtView.text = "Uid: $name\n" +
                "purchase date: $purchDate\n" +
                "country: $country\n" +
                "local: $local\n" +
                "mine: $mine\n" +
                "weight: $weight\n" +
                "paid: $paid\n" +
                "asking: $asking\n" +
                "UId: $uId\n" +
                "dimension: $dimensions\n" +
                "FilePaths:     ${filePaths.toString()}\n" +
                "downloadUrls: ${downloadUrls.toString()}\n"
        txtView.setTextColor(Color.BLACK)
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT,
        )
        txtView.layoutParams = params
        txtView.textSize = 25f
        ll.addView(txtView)

    }
}

我的代码的结果

非常感谢任何帮助,或者如果您发现我没有遵循任何其他最佳实践

如果 ConstraintLayoutRelativeLaoyut 效果更好,我不知道你为什么使用 RelativeLaoyut。所以,我把它改成了ConstraintLayout。 1st- 你的底部导航高度通常是 56dp 并且我将它添加到 56dp 如果你想要你的底部导航尺寸应用默认然后在你的 NavigationBar 的 height 中使用 ?windowActionBar。第二 - 使用嵌套滚动视图以获得更有效的结果。您的新代码如下所示。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrolly"
        android:layout_width="match_parent"
        android:layout_height="0dp"

        android:fillViewport="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_pic">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

    </androidx.core.widget.NestedScrollView>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_nav" />


</androidx.constraintlayout.widget.ConstraintLayout>

如果您遇到同样的问题。让我知道