在Android Studio中设计可滚动内容时应遵循的方法?

问题描述 投票:0回答:3

我正在使用约束布局设计我的应用程序,并将其放置在滚动视图下。我想放置更多的Card Views,但是在xml文件的预览中没有放置空间。当布局已满时,我应该在哪里拖放Buttons,TextViews等?我已经处理过文本的滚动视图,但这是另外一回事。

我知道我只能输入代码,但是当内容超出定义的屏幕尺寸时,我将无法看到我正在设计的内容时,我将很难这样做。

((如果您希望我发布屏幕截图以更好地理解问题,请在评论中告诉我)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/images"
    android:fillViewport="true"
    >
<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"
    android:background="@drawable/images"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal">

            <androidx.cardview.widget.CardView
                android:id="@+id/test_donation"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:padding="0dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#660000"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:background="#660000"
                        android:gravity="center">

                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_donate" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="DONATE"
                        android:textColor="#FFEb3b"
                        android:textSize="24sp" />

                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/test_receive"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:padding="0dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#660000"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:background="#660000"
                        android:gravity="center">

                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_receive" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RECEIVE"
                        android:textColor="#FFEb3b"
                        android:textSize="24sp" />

                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="horizontal">

            <androidx.cardview.widget.CardView
                android:id="@+id/test_chat"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:padding="0dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#660000"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:background="#660000"
                        android:gravity="center">

                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_chat" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CHAT"
                        android:textColor="#FFEb3b"
                        android:textSize="24sp" />

                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/test_logout"
                android:layout_width="160dp"
                android:layout_height="190dp"
                android:layout_margin="10dp"
                android:clickable="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:padding="0dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#660000"
                    android:gravity="center"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="130dp"
                        android:background="#660000"
                        android:gravity="center">

                        <ImageView
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:padding="10dp"
                            android:src="@drawable/ic_logout" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="LOGOUT"
                        android:textColor="#FFEb3b"
                        android:textSize="24sp" />

                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
java android xml android-layout scrollview
3个回答
0
投票

您可以尝试此android.support.v4.widget.NestedScrollView

 <android.support.v4.widget.NestedScrollView
        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="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="60dp"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:padding="8dp"
        >
    </android.support.v4.widget.NestedScrollView>

0
投票

代替scrollview,您可以使用NestedScrollView,如下所示:

<androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

        </android.support.constraint.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

0
投票

[嗯,我想您还没有尝试只添加更多的CardView(或任何其他View)...我已经复制了您的代码,并增加了CardViews的数量,它的工作原理就像魔术一样...这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <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"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:layout_editor_absoluteX="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal">

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_donation"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="DONATE"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_receive"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="RECEIVE"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal">

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_chat"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="CHAT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_logout"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="LOGOUT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal">

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_chat_other"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="CHAT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_other"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="LOGOUT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:gravity="center"
                android:orientation="horizontal">

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_chat_other_"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="CHAT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:id="@+id/test_other_"
                    android:layout_width="160dp"
                    android:layout_height="190dp"
                    android:layout_margin="10dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:padding="0dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#660000"
                        android:gravity="center"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="130dp"
                            android:background="#660000"
                            android:gravity="center">

                            <ImageView
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:padding="10dp" />
                        </LinearLayout>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="LOGOUT"
                            android:textColor="#FFEb3b"
                            android:textSize="24sp" />

                    </LinearLayout>
                </androidx.cardview.widget.CardView>
            </LinearLayout>

        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

与您所做的一样,只是没有图像,因为我没有图像的src,并且再加上四张卡,该图像就会从屏幕上移出并强制启用ScrollView s滚动。 。但是,如果尝试在特定空间中进行设置,则只应将ScrollView设置为特定高度,而不是match_parent。无论如何,ScrollView仅在有滚动内容时滚动(当然,您不必只使用卡片...您可以在ScrollView中添加任何内容,并且当某些内容超出其边界时,其滚动方式是已激活)

希望对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.