使用RelativElayout或ConstraintLayout在ScrollView内显示视图未正确显示

发布于 2025-02-13 06:23:30 字数 6948 浏览 1 评论 0 原文

我正在尝试实现这一目标...



好的,这看起来不错,没问题..但是
这是问题,如果这是在小屏幕上 我试图用 relativelayout 约束layout 实现这一目标,但效果是相同的。



XML源代码:

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_below="@id/box1"
            android:layout_marginTop="12dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_below="@id/box2"
            android:layout_marginTop="12dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/box3"
            android:gravity="bottom"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="@dimen/_12sdp"
            android:textColor="#fff"
            android:textSize="30sp"
            tools:text="This is a Textview" />
    </RelativeLayout>
</ScrollView>

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:textColor="#fff"
            android:layout_marginTop="@dimen/_12sdp"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

I am trying to achieve this...

Big Screen Small Screen

Ok this looks good no problem..BUT
here's the problem, if this is on small screen then last view is not showing properly when scrolling to the end of screen.
I've tried to achieve this with RelativeLayout and ConstraintLayout but effects are same..

enter image description here

XML Source Code:

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_below="@id/box1"
            android:layout_marginTop="12dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_below="@id/box2"
            android:layout_marginTop="12dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/box3"
            android:gravity="bottom"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="@dimen/_12sdp"
            android:textColor="#fff"
            android:textSize="30sp"
            tools:text="This is a Textview" />
    </RelativeLayout>
</ScrollView>

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:textColor="#fff"
            android:layout_marginTop="@dimen/_12sdp"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情空心 2025-02-20 06:23:30

在这里尝试一下,它应该有效,并让我知道它是否有效。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/box2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/box3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/textView1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="bottom"
            android:textColor="#fff"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Here Try This, It Should Work and let me know if it works or not.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/box2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/box3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/textView1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="bottom"
            android:textColor="#fff"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
月野兔 2025-02-20 06:23:30

这不是答案,而是要清除一些要点,为了清洁,我必须将其发布为答案...

    <?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:background="#959494"
        android:fillViewport="true"
        android:scrollbars="none">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/_12sdp">
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box1"
                android:layout_width="160dp"
                android:layout_height="160dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 1"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box2"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_marginTop="12dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/box1">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 2"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box3"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_marginTop="12dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/box2">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 3"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                android:textColor="#fff"
                android:layout_marginTop="@dimen/_12sdp"
                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/box3"
                tools:text="This is a Textview" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

如果您查看上述代码,这对小屏幕来说很好,但是在大屏幕上可以,最后一个视图只是将其位置调整到框3视图的中心和屏幕末端,但我想要的是屏幕的结束。
我们可以通过将 textView1 高度更改为0dp来实现这一目标,但是结果就像仅在较大屏幕上工作并隐藏在小屏幕上的最后一个视图相反。

当设置 textView1 高度为0dp时。结果与我说的相反。
“更大的屏幕”

This is not an answer BUT to clear some points and for cleanness, I have to post it as an answer...

    <?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:background="#959494"
        android:fillViewport="true"
        android:scrollbars="none">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/_12sdp">
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box1"
                android:layout_width="160dp"
                android:layout_height="160dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 1"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box2"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_marginTop="12dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/box1">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 2"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/box3"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:layout_marginTop="12dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/box2">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="Box 3"
                    android:textColor="#000"
                    android:textSize="30sp" />
            </com.google.android.material.card.MaterialCardView>
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                android:textColor="#fff"
                android:layout_marginTop="@dimen/_12sdp"
                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/box3"
                tools:text="This is a Textview" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

If you look at the above code this is working fine for small screens BUT on large screens, the last view is just adjusted its position to the center of the Box 3 view and end of the screen BUT what I want is to end of the screen.
We can achieve this by changing textView1 height to 0dp but then the results went opposite like working on a bigger screen only and hiding the last view on the small screen.
Small Screen Big Screen

And When setting textView1 height to 0dp.. results are the opposite as I said.
small screen bigger screen

旧人 2025-02-20 06:23:30

该解决方案使用约束布局垂直偏置

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:textColor="#fff"
            android:textSize="30sp"
            android:layout_marginTop="@dimen/_12sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            app:layout_constraintVertical_bias="1.0"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

”在此处输入图像描述”

This solution worked for me using constraint layout vertical bias

<?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:background="#959494"
    android:fillViewport="true"
    android:scrollbars="none">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/_12sdp">

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box1"
            android:layout_width="160dp"
            android:layout_height="160dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 1"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box2"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/box1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 2"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/box3"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/box2">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Box 3"
                android:textColor="#000"
                android:textSize="30sp" />
        </com.google.android.material.card.MaterialCardView>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:textColor="#fff"
            android:textSize="30sp"
            android:layout_marginTop="@dimen/_12sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box3"
            app:layout_constraintVertical_bias="1.0"
            tools:text="This is a Textview" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

enter image description here small screen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文