【问题标题】:How to fit a LinearLayout in a ScrollView?如何在 ScrollView 中安装 LinearLayout?
【发布时间】:2020-03-03 17:51:01
【问题描述】:

早些时候我问了一个问题about how to reach the last item of a scrollview,有人指出我应该使用 NestedScrollView,起初它有效,但现在不是我想要的。

我想将我的项目列表放入 ScrollView 中,这样只有屏幕的一部分可以滚动,而其他部分留在原处(3 TextView)

所以基本上我的xml文件是这样的:

<?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/cl_framgnent_detail_apero"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".ui.home.AperoDetailFragment">

    <TextView
        android:id="@+id/name_apero"
         />

    <TextView
        android:id="@+id/date_apero"
         />

    <TextView
        android:id="@+id/ingredient_title_apero"
         />

    <ScrollView
        android:id="@+id/rv_apero_ingredient"
        android:layout_width="408dp"
        android:layout_height="603dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/ingredient_title_apero"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ingredient_title_apero"
        app:layout_constraintVertical_bias="1.0">

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

</androidx.constraintlayout.widget.ConstraintLayout>

在我的 Java 代码中,我尝试使用以下代码填充我的列表:

public class AperoDetailFragment extends Fragment {

    private View root;
    private Apero detailApero;

    public AperoDetailFragment(Apero apero) {
        this.detailApero = apero;
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             final ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_detail_apero, container, false);

        TextView name = (TextView) root.findViewById(R.id.name_apero);
        name.setText(detailApero.getName());
        TextView date = (TextView) root.findViewById(R.id.date_apero);
        date.setText(detailApero.getDate());

        LinearLayout ll = (LinearLayout) root.findViewById(R.id.vertical_layout_ingredient);
        LinearLayout a = new LinearLayout(root.getContext());
        ConstraintLayout.LayoutParams lparams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
        a.setLayoutParams(lparams);

        a.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < 20; i++) {
            Button b = new Button(root.getContext());
            b.setText("Button " + i);
            a.addView(b);
        }
        ll.addView(a);

        return root;
    }
}

问题是项目覆盖了整个屏幕而不是停留在父容器(ScrollView)中:

我怎样才能让我的项目列表留在父项中?

【问题讨论】:

    标签: android android-listview android-linearlayout android-scrollview


    【解决方案1】:

    如果这是您的实际代码,那么对文本视图设置约束也可能会有所帮助。

    另外,如果您的 ScrollView 具有固定高度,那么您应该删除顶部或底部约束。因此,如果您希望它粘在底部,请移除顶部约束。

    【讨论】:

    • 是的,这是我的实际代码,除了当我尝试填充我的线性布局时,它出现在 ScrollView 之外,正如您在屏幕截图中看到的那样
    【解决方案2】:

    我通过删除 ScrollView 并使用 ListView 解决了这个问题

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多