2017-08-16 115 views
0

我想爲我的約束佈局在Guideline下面添加一些空間,但出於趣味原因它似乎沒有應用。有沒有人知道發生了什麼以及如何在Guideline(縱向和橫向方向)下應用5dp的邊距?ConstraintLayout指南中的邊距顯示不正確

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

+0

不能利潤率適用於'Guideline',請參閱[這裏](https://developer.android.com/reference/android/support /constraint/Guideline.html) – Kiya

+0

@Kiya可以做些什麼來解決這個問題呢? – MacaronLover

+0

你爲什麼首先使用它? – Kiya

回答

0

其實,事情是你已經添加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> 
+0

@MacaronLover - 將來您應該在提示編輯時解釋代碼更改。在查看對答案的編輯時,我們實際上在審閱隊列中看不到問題。 – BSMP