Android Studio 中如何消除 NoActionBar 和 ImageView 之间的差距
How do you remove the gap between NoActionBar and ImageView in Android Studio
如何消除 ConstraintLayout 中的这个间隙? top 是一个 ImageView,它有一个 top constraint to the top of parent (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>
如何消除 ConstraintLayout 中的这个间隙? top 是一个 ImageView,它有一个 top constraint to the top of parent (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>