滚动视图不适用于约束布局

时间:2018-07-28 16:24:45

标签: android layout scrollview android-constraintlayout

滚动视图不适用于约束布局,所有内容都封装在我的手机屏幕中。我应该使用scrollview作为父布局吗?我的ScrollView Layout的宽度应该是多少,我对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>

2 个答案:

答案 0 :(得分:0)

要使其正常工作,请将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,因为只有一个子视图并且它填充了父视图,但是如果您想保留自己的继承关系,只需在{{1 }}到LinearLayout

的高度

答案 1 :(得分:-1)

这是在约束布局中创建列表的非常肮脏的方式。

您应该使用ListView创建列表,而不要创建这么多的textview。

Here,您可以在ConstraintLayout中找到创建列表的很好的教程。

您可以将string.xml内的值更改为所需的值。

修改

如果要滚动条,可以在列表视图中使用这些属性

android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"

如果你想让cutum thumbScrollBar签出这个答案

https://stackoverflow.com/a/21049095/7910735