![](/img/trans.png)
[英]setup constraint in a ConstraintLayout properly while using Guideline
[英]Margin under ConstraintLayout guideline not appearing properly
我正在嘗試為我的約束布局在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>
縱向
縱向(特寫)
橫向
橫向(特寫)
您可以將邊距應用於指南:
<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_begin
和android:orientation
。 可能您應該通過縱向布局 xml 文件和橫向布局 xml 文件在其正確的 res 文件夾位置執行此操作。
實際上,問題是您添加了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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.