2017-08-16 59 views
0

我想創建一個包含複雜佈局的ConstraintLayout,但出於某種原因,它不會以預期的方式出現。有誰知道必須做些什麼才能解決這些限制並實現預期的結果?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@color/lightgrey" 
    android:weightSum="100"> 

    <com.google.android.gms.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mymap" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     app:layout_constraintBottom_toTopOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintLeft_creator="1" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_width="41dp" 
     android:layout_height="0dp" 
     tools:layout_editor_absoluteY="0dp" 
     tools:layout_editor_absoluteX="8dp" /> 

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

    <ImageView 
     android:id="@+id/imageViewSun" 
     android:importantForAccessibility="no" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_sun_black" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="guideline" 
     app:layout_constraintRight_toLeftOf="@+id/switch_stationstopmap_lighttheme" 
     app:layout_constraintHorizontal_bias="0.5" 
     android:layout_marginEnd="8dp" 
     android:layout_marginBottom="0dp" /> 

    <Switch 
     android:id="@+id/switch_stationstopmap_lighttheme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/guideline" 
     app:layout_constraintHorizontal_bias="0.5" 
     android:background="@android:color/transparent" 
     android:theme="@android:style/Theme.Material.Light" /> 

    <ImageView 
     android:id="@+id/imageViewMoon" 
     android:importantForAccessibility="no" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_moon_black" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.508" 
     app:layout_constraintLeft_toRightOf="@+id/switch_stationstopmap_lighttheme" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/guideline" 
     android:layout_marginBottom="5dp" /> 
</android.support.constraint.ConstraintLayout> 

預期結果

enter image description here

當前結果

enter image description here

期望的要求

enter image description here

  1. MapView高度需要是佈局的高度的90%
  2. 下方的3個項目的高度需要爲佈局的高度的10%
  3. Switch需要完全位於屏幕的水平中心
  4. 3項(圖像視圖和開關)需要完全位於點2的10%高度的垂直中心
  5. 太陽的ImageView需要完全水平地位於屏幕左側和開關
  6. 月亮的ImageView需要在交換機和
  7. 5DP餘量是所必需的頂部和10%的高度的底部兩者在的底部的屏幕的右手側的正中間水平是佈局(3個項目)
  8. 以上所有應導致預期結果圖像,這是我想要的。
+0

我在佈局中看到的奇怪事情之一是app:layout_constraintBottom_toTopOf =「parent」'應用於地圖。我猜,這使地圖消失了。 –

回答

1

你可以試試這個。我將您的根佈局高度更改爲android:layout_height="match_parent",然後解決了大部分問題。

<?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/colorAccent"> 
    <com.google.android.gms.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mymap" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#f00" 
     app:layout_constraintBottom_toBottomOf="@+id/guideline" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

    <ImageView 
     android:id="@+id/imageViewSun" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_name" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/switch_stationstopmap_lighttheme" 
     app:layout_constraintTop_toBottomOf="@+id/guideline" 
     /> 

    <Switch 
     android:id="@+id/switch_stationstopmap_lighttheme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     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_action_name" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toRightOf="@+id/switch_stationstopmap_lighttheme" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/guideline"/> 

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