出于某种原因,我无法弄清楚如何在 android studio 中为 android 应用程序创建滚动效果。我看过复制相同风格的视频,询问 chatgpt 一切,但我似乎不知道如何做到。
这是下面的代码,如果有人可以帮忙的话
<?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"
android:padding="1dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="always"
android:scrollbars="vertical">
<LinearLayout
android:id="@+id/fullscreen_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/top_profile_bar_linear"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:weightSum="10"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/profile_to_home_button"
android:layout_width="90dp"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:layout_weight="0.1"
>
<ImageView
android:id="@+id/back_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="2dp"
android:src="@drawable/baseline_arrow_back_24" />
<TextView
android:id="@+id/back_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="2dp"
android:text="@string/home"
android:textColor="@color/Neon_purple"
android:textSize="17dp"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingTop="2dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_weight="9.9"
android:layout_height="match_parent"
android:layout_marginRight="10dp">
<ImageView
android:id="@+id/notification_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:src="@drawable/baseline_all_inbox_24"
android:paddingLeft="270dp">
</ImageView>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/user_photo_and_details"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/top_profile_bar_linear"
android:layout_marginTop="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:weightSum="100">
<LinearLayout
android:id="@+id/profile_picture_linear"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="25dp"
>
<ImageView
android:id="@+id/user_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/rohan_image_circle">
</ImageView>
</LinearLayout>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:text="@string/user_details"
android:textColor="@color/black"
android:textSize="20dp"
android:textStyle="bold"></TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/edit_profile_button"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:focusable="true"
android:clickable="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pen_profile"
android:paddingEnd="10dp">
</ImageView>
</RelativeLayout>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="90dp"
android:layout_marginLeft="-245dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16dp"
android:text="View user info quickly">
</TextView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/edit_options_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/user_photo_and_details"
android:layout_marginTop="-48dp">
<LinearLayout
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginLeft="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Edit Options"
android:textSize="20dp">
</TextView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/option_buttons"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/edit_options_text"
android:layout_marginTop="0dp">
<LinearLayout
android:id="@+id/manageAccountButton"
android:layout_width="match_parent"
android:layout_height="75dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginStart="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/user_settings"
android:contentDescription="user Settings">
</ImageView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/managePaymentButton"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignTop="@id/manageAccountButton"
android:layout_marginTop="75dp"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/credit_card_profile">
</ImageView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/watchlistButton"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignTop="@id/managePaymentButton"
android:layout_marginTop="75dp"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginStart="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/watch_list"
android:contentDescription="Watchlist" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/supportButton"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_below="@+id/watchlistButton"
android:layout_marginTop="-1dp"
android:clickable="true"
android:focusable="true"
tools:ignore="NotSibling">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginStart="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="Watchlist"
android:src="@drawable/watch_list" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/signout_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/option_buttons"
android:layout_marginBottom="-144dp"
android:gravity="center">
<LinearLayout
android:id="@+id/signoutButton"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:background="@drawable/sign_out_card"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Sign out"
android:textSize="30dp"
android:textColor="@color/white">
</TextView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
即使注销块会超出屏幕,它也不会让我滚动。
marginBottom
中signout_layout
的负值就是问题所在。卷轴似乎运作良好。但由于设置 -144dp 作为边距,滚动视图不显示签出布局。这给人的印象是滚动不起作用。
如果此布局位于屏幕的顶部或中间,则可以对底部边距使用负值。然而,由于它在底部,所以出现了这个问题。
我建议根本不要使用 marginBottom,因为它看起来多余。即使这样做,也请使用非负值。
<RelativeLayout
android:id="@+id/signout_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/option_buttons"
android:gravity="center">
<!-- android:layout_marginBottom="-144dp"-->
此外,你的代码还可以优化很多!例如,在某些部分,您的编码如下:
<RelativeLayout>
<LinearLayout>
<TextView/>
</LinearLayout>
</RelativeLayout>
可以简化为:
<RelativeLayout>
<TextView>
<RelativeLayout>
这不仅可以提高应用程序的效率,还可以让其他人轻松阅读您的代码。