android - ConstraintLayout 准则下的边距未正确显示

标签 android xml android-constraintlayout

我正在尝试在 Guideline 下方添加一些空间对于我的约束布局,但由于某种原因,它似乎没有被应用。有谁知道发生了什么以及如何在 Guideline 下应用 5dp 的边距(对于纵向和横向)?

XML 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightgrey">
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_townmap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#09c"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9"/>

    <ImageView
        android:id="@+id/imageViewSun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:src="@drawable/ic_sun_black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/switch"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        />

    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:background="@android:color/transparent"
        android:theme="@android:style/Theme.Material.Light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/imageViewSun"
        app:layout_constraintRight_toLeftOf="@+id/imageViewMoon"
        app:layout_constraintTop_toBottomOf="@+id/guideline"/>

    <ImageView
        android:id="@+id/imageViewMoon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_moon_black"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/switch"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline"/>
</android.support.constraint.ConstraintLayout>

纵向

enter image description here

纵向(特写)

enter image description here

横向

enter image description here

横向(特写)

enter image description here

最佳答案

您可以将边距应用于指南:

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="5dp" />
虽然您可能需要调整边距 layout_constraintGuide_beginandroid:orientation因此。可能您应该通过在其正确 res 文件夹位置中的纵向布局 xml 文件和横向布局 xml 文件来执行此操作。

关于android - ConstraintLayout 准则下的边距未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45721696/

相关文章:

java - Spring - 将图像作为字节数组存储在数据库中并检索它们

Android,无限重复动画与 instrumentation.waitForIdleSync()

java - XML 解析(SAX、StAX)与 Scanner

javascript - 在javascript中出现未定义的错误

java - 在java中使用DOM创建xml

android - 在 DialogFragment 内部,RecyclerView 项目宽度在滚动前收缩

android - TextInputLayout errorDrawable 无法正确渲染矢量

android - ConstraintLayout 内的 ImageView 不起作用

android - 将 ImageView 大小与 ConstrainLayout 的比例用于响应性

android - 使用 exo 播放器添加全屏视频按钮