约束布局在顶部留下空间

3
我有一个包含布局的片段。
<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="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
    tools:context="tech.pkg.name.fragments.ReportFragment">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <Spinner
                android:id="@+id/typeSlector"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />

            <android.support.constraint.ConstraintLayout
                android:id="@+id/linearLayout2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="32dp"
                android:background="@drawable/android_date_bgd"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/fromTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/from"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/fromDateTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:drawablePadding="5dp"
                    android:padding="5dp"
                    android:text="@string/dd_mm_yy"
                    android:textStyle="bold"
                    app:layout_constraintBaseline_toBaselineOf="@+id/fromTextView"
                    app:layout_constraintEnd_toStartOf="@+id/view9"
                    app:layout_constraintHorizontal_chainStyle="spread_inside"
                    app:layout_constraintStart_toEndOf="@+id/fromTextView"
                    app:layout_constraintTop_toTopOf="@+id/fromTextView" />

                <View
                    android:id="@+id/view9"
                    android:layout_width="1dp"
                    android:layout_height="0dp"
                    android:layout_marginBottom="4dp"
                    android:layout_marginTop="4dp"
                    android:background="@android:color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/toTextView"
                    app:layout_constraintStart_toEndOf="@+id/fromDateTextView"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

                <TextView
                    android:id="@+id/toTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/to"
                    android:textStyle="bold"
                    app:layout_constraintBaseline_toBaselineOf="@+id/fromDateTextView"
                    app:layout_constraintEnd_toStartOf="@+id/toDateTextView"
                    app:layout_constraintStart_toEndOf="@+id/view9" />

                <TextView
                    android:id="@+id/toDateTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:drawablePadding="5dp"
                    android:padding="5dp"
                    android:text="@string/dd_mm_yy"
                    android:textStyle="bold"
                    app:layout_constraintBaseline_toBaselineOf="@+id/toTextView"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/toTextView" />

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

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:orientation="horizontal"
            android:padding="10dp"
            android:theme="@style/AppTheme.RadioButton"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView8"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1">

            <RadioButton
                android:id="@+id/mobileRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_weight="0.20"
                android:checked="true"
                android:text="Mobile"
                android:textSize="18sp" />

            <RadioButton
                android:id="@+id/DTHradioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.14"
                android:text="DTH"
                android:textSize="18sp" />

        </RadioGroup>

        <Button
            android:id="@+id/button_view_report"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/button_bgd"
            android:text="@string/submit"
            android:textColor="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view5"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/view5"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="15dp"
            android:background="@color/colorEditTextLine"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

        <CheckBox
            android:id="@+id/downloadCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:theme="@style/AppTheme.Checkbox"
            app:layout_constraintHorizontal_bias="0.088"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/download"
            app:layout_constraintBaseline_toBaselineOf="@+id/downloadCheck"
            app:layout_constraintHorizontal_bias="0.02"
            app:layout_constraintLeft_toRightOf="@+id/downloadCheck"
            app:layout_constraintRight_toRightOf="parent" />
    </android.support.constraint.ConstraintLayout>

它包含在

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

这里输入图片描述

但在布局编辑器中,它显示为:

这里输入图片描述

如何去掉上方的间距?


尝试这个,在我的情况下,我在常量布局中使用了以下代码: android:layout_marginBottom="?attr/actionBarSize" - Ranjan
无法工作... - Kiran Benny Joseph
不是解决方案,但尝试为不同的布局设置不同的背景颜色。这样你就可以轻松地找出哪个布局引起了问题。 - Ajil O.
我尝试了那种方法,然后我提出了这个问题。 - Kiran Benny Joseph
实际上,片段布局正在引起问题。 - Kiran Benny Joseph
问题出在你的代码片段上。布局看起来没问题。请粘贴你正在填充此视图的代码。看起来,你正在动态地向你的视图添加一些填充。 - abhishesh
4个回答

1
问题出在您的活动中,您已经填充了片段。实际上,我在我的系统上尝试过,我得到了正确的结果。
检查您的活动布局,其中使用了FrameLayout。
第二种可能性
检查您的主题是否更改了此内容。检查所有可能性。

The Same Look Like this in My phone


0

我遇到的情况类似,编辑器中没有显示在顶部的空格。

问题不在我关注的主要布局文件中,我通过viewpager显示了一些片段,编辑器对于pager的边距为0,因此编辑器预览没有显示顶部的空格。然而,正在创建/显示的片段当然具有不同的布局文件,在该布局中,由于错误地认为垂直居中布局是一个好主意(主要是因为我在睡前晚上编码,有时太累了),因此顶部有一个巨大的空间。由于viewpager的布局高度与片段的尺寸不匹配,我在尝试定位主布局文件的元素时出现了一些奇怪的行为。


0
我曾面临过类似的问题。通过查看问题中的代码片段,我发现我们都将android:fitsSystemWindows设置为True。通过删除此android:fitsSystemWindows属性,我的顶部空间被移除了。

0
尝试使用以下代码替换Spinner后的ConstraintLayout。
<android.support.constraint.ConstraintLayout
            android:id="@+id/linearLayout2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/android_date_bgd"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

@KiranBennyJoseph,我已经更新了代码,请尝试一下。日期年龄布局有一个顶部边距。我认为这是显示问题的原因。 - Tomin B Azhakathu
但我已经删除了它,这是一个设计更改。同时,我也删除了那个,但结果仍然相同。 - Kiran Benny Joseph
@KiranBennyJoseph 这是一些边距的问题。请检查一下。 - Tomin B Azhakathu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接