【问题标题】:How to use a ScrollView with a dynamic LinearLayout?如何将 ScrollView 与动态 LinearLayout 一起使用?
【发布时间】:2018-04-22 18:13:27
【问题描述】:

我正在尝试创建一个包含多个项目的 Activity,其中之一是一个 LinearLayout,它在运行时填充了其他视图。

为此目的,我的 Activity XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    tools:context="com.ibosca.thub.QuestionResultsActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/questionIcon"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:src="@drawable/ic_help_black_24dp"
                android:tint="@android:color/holo_orange_dark"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/questionText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:paddingBottom="8dp"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/questionIcon"
                app:layout_constraintVertical_bias="0.02" />

            <android.support.v7.widget.LinearLayoutCompat
                android:id="@+id/responses"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintTop_toBottomOf="@id/questionText"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>

    </ScrollView>


</android.support.constraint.ConstraintLayout>

我正在使用此 Java 代码填充响应 LinearLayoutCompat

    LinearLayoutCompat rootLayout = findViewById(R.id.responses);

    ArrayList<Response> orderedresponses = question.getOrderedResponses();

    for (int i = 0; i < orderedresponses.size(); i++){

        currentResponse = orderedresponses.get(i);

        if(i == 0){
            nextResponse = orderedresponses.get(i+1);
        }

        View child = getLayoutInflater().inflate(R.layout.response, null);

        TextView responseText = child.findViewById(R.id.responseText);
        TextView votesText = child.findViewById(R.id.votesText);

        responseText.setText(currentResponse.getText());
        String votesInfo = getResources().getString(R.string.votesPercentQuestionResult, Integer.toString(currentResponse.getVotes()) , Float.toString(currentResponse.getPercentVote()));
        //votesText.setText(currentResponse.getVotes() + " vots · " + currentResponse.getPercentVote() +"%");
        votesText.setText(votesInfo);

        if(i == 0 && currentResponse.getVotes() != 0 && nextResponse.getVotes() < currentResponse.getVotes()){
            //Set star icon
            ImageView icon = child.findViewById(R.id.icon);
            icon.setImageDrawable(getResources().getDrawable(R.drawable.ic_stars_black_24dp));

        }

        rootLayout.addView(child);

    }

而且,最后一段代码中使用的响应 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <ImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/icon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_insert_comment_black_24dp"
        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" />

    <TextView
        android:id="@+id/responseText"
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight=".90"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/icon"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/votesText"
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight=".90"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:singleLine="true"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="@+id/icon"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/icon"
        app:layout_constraintTop_toBottomOf="@id/responseText" />

</android.support.constraint.ConstraintLayout>

问题:

在我的尝试中,我意识到在 Response 的 LinearLayoutCompat 之后有一个额外的空白空间,这会在实际上不需要滚动时导致滚动。

我无法弄清楚这个空间是从哪里来的,但似乎这个空白空间是随着响应线性布局长大的,所以当响应线性布局里面的内容很小时,空白空间很小,随着它的增长,空白也随之增加。

我的目标是删除这个额外的空白空间,并且仅在需要时才具有滚动视图,即当线性布局大于屏幕尺寸时。

我该如何解决?

编辑:问题的屏幕截图

正如您在第一个屏幕截图中看到的那样,内容适合屏幕,但是 android 在线性布局的底部添加了额外的空间,从而导致了一个陌生的空白空间。

【问题讨论】:

    标签: android android-linearlayout android-scrollview


    【解决方案1】:

    在您的 .xml 文件中编辑或删除这些行,您的问题将得到解决:

                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
    

    【讨论】:

    • 感谢您的回复!你能指定我应该在哪里删除这些行吗?这些行出现在我的 XML 中不止一处。
    • 欢迎您,我们使用margin来在事物之间留出空间,所以您应该根据需要编辑或删除margin,例如这里,您可以在ImageView和TextView处删除margin
    • 我认为这是一个误解。空白区域在线性布局的底部,而不是元素之间,所以我认为这个边距限制不是问题。我将附上一张图片作为编辑来澄清这一点。
    • 是的,在看到你的屏幕截图后,我明白到底发生了什么,我也不知道是什么问题,但我想也许问题可以通过改变 ConstraintLayout 和 ScrollView 的高度来解决,你试过吗?用 match_parent 改变 wrap_content ?
    【解决方案2】:

    你需要:

    1. 在根布局中将 android:layout_height="wrap_content" 更改为 android:layout_height="match_parent"
    2. 在 ScrollView 中将 android:layout_height="wrap_content" 更改为 android:layout_height="match_parent"
    3. app:layout_constraintBottom_toBottomOf="parent" 添加到您的LinearLayoutCompat

    这是代码

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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"
        tools:context="com.ibosca.thub.QuestionResultsActivity">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <ImageView
                    android:id="@+id/questionIcon"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:src="@drawable/ic_help_black_24dp"
                    android:tint="@android:color/holo_orange_dark"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />
    
                <TextView
                    android:id="@+id/questionText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:gravity="center"
                    android:paddingBottom="8dp"
                    android:textColor="@android:color/black"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/questionIcon"
                    app:layout_constraintVertical_bias="0.02" />
    
                <android.support.v7.widget.LinearLayoutCompat
                    android:id="@+id/responses"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:orientation="vertical"
                    app:layout_constraintTop_toBottomOf="@id/questionText"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintVertical_bias="0.0"/>
    
            </android.support.constraint.ConstraintLayout>
    
        </ScrollView>
    
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      最后,我自己解决了问题。

      我使用 Android Studio Layout Inspector 来准确查看我的布局中的哪一部分占用的空间超出了需要。您可以在文档中找到更多信息:https://developer.android.com/studio/debug/layout-inspector.html#open_the_layout_inspector

      问题出在约束布局上,所以我尝试将约束布局更改为线性布局,这解决了问题。

      这是最终代码:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.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"
          tools:context="com.ibosca.thub.QuestionResultsActivity">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fillViewport="true">
      
              <android.support.v7.widget.LinearLayoutCompat
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
      
                  <ImageView
                      android:id="@+id/questionIcon"
                      android:layout_width="80dp"
                      android:layout_height="80dp"
                      android:layout_marginBottom="8dp"
                      android:layout_marginEnd="8dp"
                      android:layout_marginStart="8dp"
                      android:layout_marginTop="8dp"
                      android:layout_gravity="center"
                      android:src="@drawable/ic_help_black_24dp"
                      android:tint="@android:color/holo_orange_dark"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toTopOf="parent"
                      app:layout_constraintVertical_bias="0.0" />
      
                  <TextView
                      android:id="@+id/questionText"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="8dp"
                      android:layout_marginEnd="8dp"
                      android:layout_marginStart="8dp"
                      android:layout_marginTop="8dp"
                      android:gravity="center"
                      android:paddingBottom="8dp"
                      android:textColor="@android:color/black"
                      android:textSize="20sp"
                      android:textStyle="bold"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintEnd_toEndOf="parent"
                      app:layout_constraintStart_toStartOf="parent"
                      app:layout_constraintTop_toBottomOf="@id/questionIcon"
                      app:layout_constraintVertical_bias="0.02" />
      
                  <android.support.v7.widget.LinearLayoutCompat
                      android:id="@+id/responses"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical"
                      app:layout_constraintBottom_toBottomOf="parent"
                      app:layout_constraintTop_toBottomOf="@id/questionText"
                      app:layout_constraintVertical_bias="0.0" />
      
              </android.support.v7.widget.LinearLayoutCompat>
      
          </ScrollView>
      
      
      </android.support.constraint.ConstraintLayout>

      希望这可以帮助其他有类似问题的人!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-19
        • 1970-01-01
        • 2012-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        相关资源
        最近更新 更多