【问题标题】:Scroll view doesn't work with constraint layout滚动视图不适用于约束布局
【发布时间】:2019-01-05 11:28:07
【问题描述】:

滚动视图不适用于约束布局,所有内容都包含在我的手机屏幕中。我应该使用滚动视图作为父布局吗?我的滚动视图布局的宽度应该是多少,我对 wrap_content 和 match_parent 感到困惑............ ..................................................... …… This is what i am getting

    <?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"
    android:background="#1a1a22"
    tools:context="com.example.mrfrag.fullchargealarm.Settings">

   <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/scrollView6" android:fillViewport="true"
      android:layout_marginTop="8dp"
      app:layout_constraintTop_toTopOf="parent"
      android:layout_marginEnd="8dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
       android:layout_marginStart="8dp"
      app:layout_constraintBottom_toBottomOf="parent"
      android:layout_marginBottom="8dp"
      android:scrollbars = "vertical"
      android:scrollbarStyle="insideInset"
      app:layout_constraintHorizontal_bias="0.0"
      app:layout_constraintVertical_bias="0.0">
     <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FIRE1" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/textVie2321"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/textVie23"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/textVie45"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/tesadasdxtVie3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/teasasdxtVie3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/texasastVie3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
        android:id="@+id/textVsase3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:text="FFIRE" />

        <TextView
            android:id="@+id/textasdasdVsase3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/tasdasdextVsase3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/texastVsase3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/texVsase3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />
        <TextView
            android:id="@+id/tetVsase3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="FFIRE" />

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

【问题讨论】:

  • 尝试使用 NestedScrollView。我不确定它是否有效,但值得一试

标签: android layout scrollview android-constraintlayout


【解决方案1】:

为了让它工作,让 ScrollView 成为父视图,宽度和高度为match_parent(这样它就填满了屏幕)。然后将下一个布局放入其中,宽度为match_parent(因此它是父级的宽度)和高度为wrap_content(因此它可以高于父级高度并滚动显示内容)。

wrap_content 是做什么的?它可以让视图达到显示其内容所需的高度。如果它比父级高,那么 ScrollView 允许您上下滚动以查看内容。

例如:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- this could be a constraint layout instead if you want -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- STUFF -->

    </LinearLayout>
</ScrollView>

在您的情况下,父 ConstraintLayout 不是必需的,因为只有一个子视图并且它正在填充父视图,但是如果您想保留您拥有的层次结构,只需将 ScrollView 内的 LinearLayout 立即更改为wrap_content的高度

【讨论】:

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