-1

如何消除 ConstraintLayout 中的这个间隙?顶部是一个 ImageView,对父级(ConstraintLayout)的顶部有一个顶部约束。

工具栏和横幅之间的顶部间隙

这就是我的 styles.xml 的样子:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

我的 activity_main.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="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/headerImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/header" />

    <Button
        android:id="@+id/addlog_btn"
        style="@android:style/Widget.DeviceDefault.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="108dp"
        android:background="@drawable/newlogbtn"
        android:fontFamily="@font/roboto_regular"
        android:onClick="createNewLog"
        android:paddingTop="72sp"
        app:layout_constraintStart_toStartOf="@+id/headerImg"
        app:layout_constraintTop_toTopOf="@+id/headerImg" />

    <Button
        android:id="@+id/addlog_btn2"
        style="@android:style/Widget.DeviceDefault.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:background="@drawable/settingsbtn"
        android:fontFamily="@font/roboto_regular"
        android:paddingTop="72sp"
        app:layout_constraintEnd_toEndOf="@+id/headerImg"
        app:layout_constraintTop_toTopOf="@+id/addlog_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的最终目标是这样的:

最终目标绘图

如果可能的话,我想通过编辑 XML 来实现这一点,我尝试更改父级的边距和填充,但我没有做对,或者它似乎不起作用。

4

1 回答 1

0

android:scaleType="centerCrop"您可以通过添加到横幅来消除此间隙,ImageView以便它将填充整个分配给它的房间。

因此布局将在此更改之后:

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

    <ImageView
        android:id="@+id/headerImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/header" />

    <Button
        android:id="@+id/addlog_btn"
        style="@android:style/Widget.DeviceDefault.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="108dp"
        android:background="@drawable/newlogbtn"
        android:fontFamily="@font/roboto_regular"
        android:onClick="createNewLog"
        android:paddingTop="72sp"
        app:layout_constraintStart_toStartOf="@+id/headerImg"
        app:layout_constraintTop_toTopOf="@+id/headerImg" />

    <Button
        android:id="@+id/addlog_btn2"
        style="@android:style/Widget.DeviceDefault.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:background="@drawable/settingsbtn"
        android:fontFamily="@font/roboto_regular"
        android:paddingTop="72sp"
        app:layout_constraintEnd_toEndOf="@+id/headerImg"
        app:layout_constraintTop_toTopOf="@+id/addlog_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
于 2020-04-26T23:26:14.747 回答