Android Studio -片段内的约束布局未正确对齐

x4shl7ld  于 2022-12-25  发布在  Android
关注(0)|答案(1)|浏览(112)

我尝试在我的应用程序中使用片段内部的约束布局。问题是,正如你从下面的图片中看到的,无论我做什么,布局似乎都是从屏幕的一半开始的。
这是我编写的fragment_progress. xml的代码:

<?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="wrap_content"
    android:layout_gravity="start"
    android:background="@color/app_purple"
    android:textAllCaps="false"
    tools:context=".ProgressFragment">

        <TextView
            android:id="@+id/fg_progress_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:text="Your Progress"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textSize="34sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/fg_progress_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="50dp"
            android:layout_marginTop="32dp"
            android:text="Current Goal: "
            android:textColor="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_title" />

        <EditText
            android:id="@+id/fg_progress_header2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="50dp"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/fg_progress_header"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_title" />

        <TextView
            android:id="@+id/fg_progress_goalprogress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="50dp"
            android:text="Goal Progress:"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_header" />

        <EditText
            android:id="@+id/fg_progress_points"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:text=""
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_goalprogress" />

        <TextView
            android:id="@+id/fg_progress_points2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            android:text="Points"
            android:textColor="#FFFFFF"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/fg_progress_points"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_goalprogress" />

        <ProgressBar
            android:id="@+id/fg_progress_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_points" />

        <TextView
            android:id="@+id/fg_progress_awardstitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="50dp"
            android:text="Awards Unlocked:"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_bar" />

        <EditText
            android:id="@+id/fg_progress_award1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text=""
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_awardstitle" />

        <TextView
            android:id="@+id/fg_progress_badgestitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="50dp"
            android:text="Badges Obtained:"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_award1" />

        <EditText
            android:id="@+id/fg_progress_badge1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text=""
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_badgestitle" />

        <TextView
            android:id="@+id/fg_progress_whotitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="50dp"
            android:text="The World Health Organization Recommends:"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_badge1" />

        <TextView
            android:id="@+id/fg_progress_whoheader1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="Know that it is okay to feel how you are feeling."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whotitle" />

        <TextView
            android:id="@+id/fg_progress_whocontent1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text="It is normal during this crazy time to experience feelings of sadness, anger frustration, stress, or all of the above. You are allowed to feel this way and to communicate with others how you are feeling."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whoheader1" />

        <TextView
            android:id="@+id/fg_progress_whoheader2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="Maintain a routine."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whocontent1" />

        <TextView
            android:id="@+id/fg_progress_whocontent2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text="Start your day at about the same time each day. Set a goal for coursework to be completed for each morning and afternoon. Maintain adequate nutrition by eating three healthy meals per day. Try to get in at least one physical activity each day."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whoheader2" />

        <TextView
            android:id="@+id/fg_progress_whoheader3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="Practice good sleep hygiene."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whocontent2" />

        <TextView
            android:id="@+id/fg_progress_whocontent3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text="Maintain a consistent sleep schedule. The goal should be 7-9 hours per night. Limit screen time in the evening, and avoid caffeine starting in the afternoon."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whoheader3" />

        <TextView
            android:id="@+id/fg_progress_whoheader4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="Connect with others."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whocontent3" />

        <TextView
            android:id="@+id/fg_progress_whocontent4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text="It is easy to quickly feel lonely and secluded from others during this stay-at-home period. Make an effort to stay socially connected by engaging in regular video or phone calls with friends and family."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whoheader4" />

        <TextView
            android:id="@+id/fg_progress_whoheader5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="Take a break."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whocontent4" />

        <TextView
            android:id="@+id/fg_progress_whocontent5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="24dp"
            android:text="Take time for yourself each day. Step away from the news and from your coursework to do something you enjoy and that you find relaxing or rejuvenating."
            android:textColor="#FFFFFF"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whoheader5" />

        <TextView
            android:id="@+id/fg_progress_fblink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            android:layout_marginBottom="8dp"
            android:text="(https://www.facebook.com/coronavirus_info/core_tips)"
            android:textAlignment="center"
            android:textColor="#FFFFFF"
            android:textSize="10sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fg_progress_whocontent5" />

</androidx.constraintlayout.widget.ConstraintLayout>

它曾经包含一个FrameLayout和一个ScrollView,但是我删除了它们以简化代码,因为它们没有帮助。下面是两张图片,展示了手机屏幕应该是什么样子以及它实际上是什么样子:(请忽略操作栏和底部导航菜单)
Android Studio

电话

我几乎尝试了所有的方法,从切换match_parentwrap_content,设置元素或布局本身的边距,改变重力等。唯一有效的是设置一个负的底部边距**-800dp**到约束布局,但这破坏了滚动功能,添加一个ScrollView也没有帮助。我不知道为什么会发生这种情况。我只需要第一个TextView转到屏幕的顶部。其他的一切都被适当地约束,所以他们会跟随。
有什么想法吗?
先谢了!

y4ekin9u

y4ekin9u1#

这里的问题不在于片段的XML代码,而在于宿主Activity布局文件。由于您在Activity中托管用于底部导航的片段,因此如果检查XML代码,您可能会发现Activity的父视图中有android:paddingTop="?attr/actionBarSize"此代码。删除填充后,它将正常工作。

相关问题