简体   繁体   English

ScrollView 不使用 ConstraintLayout 和指南滚动

[英]ScrollView not scrolling with ConstraintLayout and guideline

I have to make a layout with an image on top and text above.我必须用顶部的图像和上面的文本进行布局。 The images height has to be 40% of the root layout height.图像高度必须是根布局高度的 40%。 The text has variable height.文本具有可变高度。 Therefore the whole thing has to be put in a ScrollView.因此,整个事情必须放在 ScrollView 中。

Here is my code:这是我的代码:

<?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="@color/colorTutorialGrey"
>


<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true"
    >

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

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

        <ImageView
            android:id="@+id/tutorial_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/guideline"
            tools:src="@drawable/splash_bg"
            />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="32dp"
            app:layout_constraintTop_toBottomOf="@id/guideline"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            >

            <TextView
                android:id="@+id/tutorial_title"
                style="@style/Tutorial.Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:text="Alle Neuerungen der App auf einen Blick"
                />

            <TextView
                android:id="@+id/tutorial_description"
                style="@style/Tutorial.Description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet. no sea takimata sanctus est Lorem ipsum dolor sit amet."
                />

        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

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

The layout is like it is desired, but as soon as i put in the Guideline, the ScrollView does not scroll anymore.布局就像是想要的一样,但是一旦我放入了指南,ScrollView 就不再滚动了。

What mistake do i make and how can i achieve the desired layout and behaviour?我犯了什么错误,如何实现所需的布局和行为?

The guideline and the image view must have the same ConstraintLayout as a parent.指南和图像视图必须与父视图具有相同的ConstraintLayout Move the guideline just above the image view and you should be set.将指南移动到图像视图的正上方,您应该已经设置好了。

On a side note, you probably don't need the outermost ConstraintLayout .附带说明一下,您可能不需要最外面的ConstraintLayout You can also probably get rid of the nested LinearLayout for a flatter layout.您也可以摆脱嵌套的LinearLayout以获得更平坦的布局。


The guideline you have set is based upon a percentage.您设置的准则基于百分比。 Unfortunately, this percentage seems to be a fraction of the total scrollable height of the view and not just what you see on the screen.不幸的是,这个百分比似乎是视图总可滚动高度的一小部分,而不仅仅是您在屏幕上看到的。

You want the image to take up 40% of the scroll view's visible portion.您希望图像占据滚动视图可见部分的 40%。 I suggest that you set the guideline in the XML to be a fixed size ( app:layout_constraintGuide_begin="100dp" for instance) and change the guideline placement in code doing something like the following:我建议您将 XML 中的指南设置为固定大小(例如app:layout_constraintGuide_begin="100dp" )并更改代码中的指南位置,执行如下操作:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrollable);
    final ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);

    layout.post(new Runnable() {
        @Override
        public void run() {
            final ScrollView scrollView = (ScrollView) layout.findViewById(R.id.scrollView);
            final Guideline guideline = (Guideline) findViewById(R.id.guideline);
            final ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
            lp.guideBegin = (int) ((scrollView.getHeight()) * 0.4);
            guideline.setLayoutParams(lp);
        }
    });
}

I would suggest using ConstraintSet#setGuidelineBegin , but I can't get that to work.我建议使用ConstraintSet#setGuidelineBegin ,但我无法ConstraintSet#setGuidelineBegin工作。 You may have better luck.你可能有更好的运气。

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

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