簡體   English   中英

Android Studio中如何消除NoActionBar和ImageView之間的差距

[英]How do you remove the gap between NoActionBar and ImageView in Android Studio

如何消除 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 來實現這一點,我嘗試更改父級的邊距和填充,但我沒有做對,或者它似乎不起作用。

您可以通過將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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM