【问题标题】:implementing constraint layout in scrollview在滚动视图中实现约束布局
【发布时间】:2018-09-27 05:42:36
【问题描述】:

这样实现时滚动视图不起作用

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 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:fillViewport="true"
    tools:context=".MainActivity">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/Constraint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="1.05" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@id/guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />
    </android.support.constraint.ConstraintLayout>

</ScrollView>

我看到了一篇文章,我们可以通过将元素增加到 100% 以上来将元素锚定到指南,但是当我使用它时,滚动不起作用,即使 textview 不再可见,我该如何解决这个问题

【问题讨论】:

  • 您能详细说明一下吗? RequiredCurrent 的屏幕截图会更有帮助。
  • 如果在 TextView 中使用 app:layout_constraintTop_toTopOf="@id/guideline" 而不是 app:layout_constraintTop_toTopOf="parent" 会怎样。
  • @JeelVankhede 不能那样工作
  • 文本视图位于约束布局的底部,就像指南超过 100% 我在滚动视图中使用了约束布局,因为如果我滚动我必须看到文本视图不能弄清楚如何做滚动不起作用@SanthoshJoseph
  • 是的,这就是我想要的@SanthoshJoseph

标签: android scrollview android-scrollview android-constraintlayout


【解决方案1】:

实际上,当有足够的元素滚动时,您的代码应该可以工作,尝试在文本视图中添加更高的margintop,您会看到结果:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:gravity="center_horizontal"
    tools:context=".MainActivity">

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

        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="630dp"
            android:layout_marginBottom="32dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintGuide_percent="1.0"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="600dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@id/guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />


    </android.support.constraint.ConstraintLayout>
</ScrollView>

【讨论】:

    【解决方案2】:

    你可以试试BottomSheet 吗?

    【讨论】:

    • 我在约束布局中从上到下有一个连续的组件结构我希望在约束布局中滚动,以便滚动时底部元素也可用谢谢
    【解决方案3】:

    将以下线性布局添加到滚动视图中,并在此线性布局中添加约束布局

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

    我认为它会起作用

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      相关资源
      最近更新 更多