2017-06-19 225 views
0

我有一個約束佈局的指導,我用它來錨定LinearLayout保持在它的左邊,如下所示:爲什麼我的佈局忽略約束佈局指南?

enter image description here

所以,你可以看到Guideline位權中,LinearLayout之間在左側和ImageView右側。

現在,當我運行的應用程序,並設置技能集的文字導師類型位置相當大的,它跨越了方針作爲ImageView背後如下:

enter image description here

注意:這是不是一個真正的人的數據,但模擬數據)

如果您看到XML,您會發現LinearLayout的確有意錨定to_left_ofGuideline但這種情況不會發生。

那麼,這裏有什麼問題?約束佈局中是否存在錯誤或者我錯過了什麼?


佈局以供參考:

<?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:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginStart="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginEnd="10dp" 
    android:background="@color/lightGrey" 
    android:layout_marginBottom="10dp" 
    android:layout_marginTop="10dp" 
    android:paddingBottom="10dp" 
    android:elevation="2dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginLeft="16dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="8dp" 
     android:layout_marginEnd="8dp" 
     app:layout_constraintHorizontal_bias="0.0" 
     android:id="@+id/linearLayout" 
     tools:layout_editor_absoluteY="16dp"> 

     <TextView 
      android:text="@string/tutor_name" 
      android:textStyle="bold" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tutor_name"/> 

     <TextView 
      android:text="@string/tutor_skill_set" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:id="@+id/skill_set"/> 

     <TextView 
      android:text="@string/tutor_types" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:id="@+id/tutor_types" /> 

     <TextView 
      android:text="@string/tutor_location" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/location" 
      android:layout_marginTop="12dp" /> 

    </LinearLayout> 



    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     app:srcCompat="@android:color/holo_blue_light" 
     android:id="@+id/display_pic" 
     android:adjustViewBounds="false" 
     android:scaleType="centerCrop" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginEnd="16dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="8dp" 
     app:layout_constraintLeft_toLeftOf="@+id/guideline" 
     app:layout_constraintHorizontal_bias="1.0" /> 

    <com.iarcuschin.simpleratingbar.SimpleRatingBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tutor_rating" 
     android:layout_below="@+id/display_pic" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     app:srb_starSize="13dp" 
     app:srb_numberOfStars="5" 
     app:srb_borderColor="@color/blue" 
     app:srb_fillColor="@color/blue" 
     app:srb_starBorderWidth="1" 
     app:srb_isIndicator="true" 
     android:layout_marginRight="0dp" 
     app:layout_constraintRight_toRightOf="@+id/display_pic" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/display_pic" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="@+id/guideline" 
     app:layout_constraintHorizontal_bias="1.0" /> 

    <TextView 
     android:id="@+id/tutor_requested_time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:text="Requested time" 
     android:textStyle="italic" 
     android:layout_marginTop="24dp" 
     app:layout_constraintTop_toBottomOf="@+id/tutor_rating" 
     app:layout_constraintRight_toLeftOf="@+id/guideline" 
     android:layout_marginRight="8dp" 
     app:layout_constraintLeft_toLeftOf="@+id/linearLayout" 
     app:layout_constraintHorizontal_bias="0.0" /> 

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


</android.support.constraint.ConstraintLayout> 
+0

明顯的解決方案是在LinearLayout中將'android:layout_width =「wrap_content」''更改爲'android:layout_width =「0dp」' – Selvin

+0

@Selvin:讓我試試這個... –

+0

它將作爲'0dp'意思就像「基於視角的計算」,而wrap_content意味着「基於兒童的計算」 – Selvin

回答

0

更改LinearLayoutandroid:layout_width="0dp"android:layout_width="wrap_content"財產。

這將起作用,因爲將layout_width設置爲0dp意味着LinearLayout的寬度將根據對其施加的約束來計算。而如果寬度設置爲wrap_content,寬度將根據其子視圖的大小計算。