2017-05-20 9 views
2

내가 constraintLayout 및 layout_constraintDimensionRatio가 = "1 : 1"사용 워킹 결과로ConstraintLayout layout_constraintDimensionRatio하지

(폭, 높이 0dp (match_constraint) warp_content이다), I가되도록 폭과 높이를 예상 1 : 1이지만 작동 안함.

무엇이 잘못 되었나요 ??

첨부 된 코드와 스크린 샷.

screenshot

<?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="match_parent"> 

    <TextView 
     android:id="@+id/t1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:background="@android:color/holo_blue_bright" 
     android:gravity="center" 
     android:text="Hello World!11" 
     app:layout_constraintDimensionRatio="1:1" /> 

</android.support.constraint.ConstraintLayout> 

나는 Constraintlayout에 대한 안드로이드 개발자 사이트를 인용. https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

는 "비 :: 또한 다른 하나의 비율로 위젯의 한 차원을 정의 할 수 있습니다. 그렇게하기 위해서는, 즉 (제약 차원이 0dp로 설정 적어도 하나 있어야합니다, MATCH_CONSTRAINT)과 소정의 비율로 layout_constraintDimentionRatio 속성 설정 예를 들어.

 <Button android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       app:layout_constraintDimensionRatio="1:1" /> 

그 폭과 동일하게 버튼의 높이를 설정한다. "

->하지만 작동하지 않았습니다. 당신은 잊지

+0

'ConstrainLayout'의 버전은 무엇입니까? – Shailesh

+0

안녕 Shailesh. 1.0.2를 사용했습니다. – pistolcaffe

+0

'match_parent'는 지원되지 않습니다. 'ConstraintLayout'을 적절하게 설정하려면'0dp'를 설정해야합니다. 이것을 확인하십시오 http://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout?rq=1 – Shailesh

답변

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="match_parent"> 

    <TextView 
     android:id="@+id/t1" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:background="@android:color/holo_blue_bright" 
     android:gravity="center" 
     android:text="Hello World!11" 
     app:layout_constraintDimensionRatio="1" /> 

</android.support.constraint.ConstraintLayout> 

0dp 만 ConstraintLayout의 아이 뷰에 적용을 추가 할 수 있습니다. 모든 뷰는 상위 뷰의 속성 규칙을 적용해야합니다.

+0

폭을 0dp로 설정하는 것을 잊어 버렸습니다. 그러나 안드로이드 개발자 사이트의 예제가 삽입했을 때만 작동하는 것을 보여주는 이유는 모르겠습니다. – pistolcaffe

-1

가장자리에 제약 조건을 추가하면 제약 조건이 적용됩니다. 마지막 줄을 확인하십시오.

Studio에서 디자인보기를 사용하고 개체간에 제약 조건을 끌어다 놓을 수도 있습니다.

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Available chats" /> 

<ListView 
    android:id="@+id/listChats" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_constraintTop_toBottomOf="@+id/textView"/> 
+0

답변으로 문제가 어떻게 해결됩니까? 소스 코드의 마지막 줄은 단 하나의 제약을 나타내지 만, 비율을 적용하려면 더 많은 제약이 있어야합니다! –

관련 문제