【问题标题】:Margin under ConstraintLayout guideline not appearing properlyConstraintLayout 准则下的边距未正确显示
【发布时间】:2020-07-21 15:19:20
【问题描述】:

我试图在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>

纵向方向

纵向(特写)

横向

横向(特写)

【问题讨论】:

  • 您不能将边距应用于Guideline,请参考this
  • @Kiya 那么可以做些什么来解决这个问题呢?
  • 你为什么首先使用它?
  • @Kiya 此布局以前有一个 PercentRelativeLayout,现在已弃用。
  • 您可以使用带有权重的LinearLayout

标签: android xml android-constraintlayout


【解决方案1】:

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

<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 文件来执行此操作。

【讨论】:

    【解决方案2】:

    实际上,问题是您添加了Guideline,并且您正在使用app:layout_constraintGuide_percent="0.9"。这意味着Guideline将根据父布局设置,所以当你改变方向时它会看起来很奇怪。

    使用以下代码解决您的问题:

    <?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_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/imageViewSun"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <ImageView
            android:id="@+id/imageViewSun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/ic_sun_black"
            app:layout_constraintTop_toBottomOf="@+id/map_stationmap_lighttheme"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/switch" />
    
        <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:layout_marginTop="5dp"
            app:layout_constraintTop_toBottomOf="@+id/map_stationmap_lighttheme"
            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"/>
    
        <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"
            android:layout_marginTop="5dp"
            app:layout_constraintTop_toBottomOf="@+id/map_stationmap_lighttheme"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/switch"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • @MacaronLover - 将来您应该在建议编辑时解释代码更改。在查看对答案的编辑时,我们实际上并没有在审核队列中看到该问题。
    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多