简体   繁体   English

ScrollView和ConstraintLayout不起作用

[英]ScrollView and ConstraintLayout not working

I have added the <ScrollView> code on activity.xml 我在activity.xml上添加了<ScrollView>代码

There is my xml code: 有我的xml代码:

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbarStyle="insideOverlay">
    <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:id="@+id/scrooll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout5"
        android:layout_width="0dp"
        android:layout_height="59dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:focusable="false"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3"
        app:layout_constraintVertical_bias="0.0">

        <EditText
            android:id="@+id/numero"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:focusable="false"
            android:hint="Numero"
            android:inputType="none"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="-195dp" />
    </android.support.design.widget.TextInputLayout>


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="119dp"
        android:layout_height="101dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:indeterminate="true"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.491"
        tools:visibility="visible" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Destinatario:"
        android:textSize="18sp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
        app:layout_constraintVertical_bias="0.0" />          

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:entries="@array/country_arrays"
        android:spinnerMode="dialog"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout5"
        app:layout_constraintVertical_bias="0.013" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="Aggiorna"
        android:text="Aggiorna"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner"
        app:layout_constraintVertical_bias="0.0" />

but after this operation the layout is not centered on phone: 但是在执行此操作后,布局不会以手机为中心:

在此处输入图片说明

and before scrollview: 在scrollview之前:

在此处输入图片说明

Any element is anchored perfectly =( 任何元素都完美锚定=(

this is my first application, please be patient =)this is my first application, please be patient =)this is my first application, please be patient =) 这是我的第一次申请,请耐心=)这是我的第一次申请,请耐心=)这是我的第一次申请,请耐心=)

I think that's working as intended. 我认为这按预期工作。 Here's what you have: 这是您所拥有的:

<scrollview>
  <constraintlayout>
    ... other widgets ...
    <spinner>
    ... other widgets ...
  </constraintlayout>
</scrollview>

With the spinner constrained to the parent (the constraintlayout). 将微调器约束到父级(约束布局)。

So... if you put the constraintlayout in a scrollview, obviously it can be taller than the screen -- that's the point of scrollview, to handle this scenario. 所以...如果将constraintlayout 放在 scrollview中,显然它可以比屏幕高-这就是scrollview的意义所在。 The spinner is not constrained to the screen , it's constrained to the parent. 微调器不限于屏幕 ,而是限于父屏幕 So if the parent happens to be taller than the screen, well, the spinner will be centered in the parent, that's all. 因此,如果父母恰好比屏幕高,那么微调器将位于父母的中心,仅此而已。

What you seem to want is a little different: 您似乎想要的有点不同:

<constraintlayout>
    <scrollview>
        <constraintlayout>
            ... other widgets ...
        </constraintlayout>
    </scrollview>
    <spinner>
</constraintlayout>

With scrollview being set to whatever size you want, and the spinner being declared after it (to be drawn afterward). 将scrollview设置为所需的大小,然后在其后声明微调器(稍后绘制)。 The spinner can then be set as in your code, centered inside the parent, but this time the parent will be corresponding to the visible screen. 然后可以像在代码中那样将微调器设置为在父级内部居中,但是这次父级将对应于可见屏幕。 Then, inside the scrollview, you put another constraintlayout containing your content. 然后,在滚动视图内部,放置另一个包含内容的约束布局。

The scrollview's child is linear layout not constraint layout. scrollview的子级是线性布局,而不是约束布局。 Use linear layout(with orientation) instead of constraint layout 使用线性布局(具有方向)而不是约束布局

I edited your code little bit :) 我稍微编辑了你的代码:)

<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<LinearLayout android:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrooll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout5"
        android:layout_width="0dp"
        android:layout_height="59dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:focusable="false"
        android:visibility="invisible"
        >

        <EditText
            android:id="@+id/numero"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="Numero"
            android:inputType="none" />
    </android.support.design.widget.TextInputLayout>


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="101dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:indeterminate="true"
        android:visibility="visible"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Destinatario:"
        android:textSize="18sp"
        android:visibility="invisible" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:entries="@array/country_arrays"
        android:spinnerMode="dialog"
        android:visibility="invisible"
       />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="Aggiorna"
        android:text="Aggiorna"
        android:visibility="invisible"
        />

</LinearLayout></ScrollView>

and also scroll view is unnecessary too 而且也不需要滚动视图

i think it's better for you to use a "Guide Line" to make your activity or project responsive. 我认为最好使用“指导线”来使您的活动或项目具有响应性。

Thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM