2017-11-16 21 views
3

ConstrainLayout을 사용하려고하지만 사용하기가 어렵습니다. 나는 스크린과 같은 설정을위한 레이아웃을 가지고 있으며 매우 간단하고 구현하기 쉬운 LinearLayout과 RelativeLayout을 사용하고 있습니다. 여기 레이아웃입니다 :LinearLayout 및 RelativeLayout을 ConstraintLayout으로 변환하는 방법은 무엇입니까?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/tv_stay_awake" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Stay awake" 
      android:padding="10dp"/> 
     <Switch 
      android:id="@+id/switch_stay_awake" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true"/> 
    </RelativeLayout> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/tv_notification" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Enable Notification" 
      android:padding="10dp"/> 
     <Switch 
      android:id="@+id/switch_notification" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true"/> 
    </RelativeLayout> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/tv_location" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Enable location" 
      android:padding="10dp"/> 
     <Switch 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true"/> 
    </RelativeLayout> 

</LinearLayout> 

이는 모습입니다 : enter image description here

가 지금은 동일한 설정의 UI를 구축하기를 원하지만 ConstrainLayout를 사용하고 여기에 ConstrainLayout 인이 UI를 설정을 내가 가진 .

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <TextView 
     android:id="@+id/tv_stay_awake" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Stay awake"/> 
    <Switch 
     android:id="@+id/switch_stay_awake" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintRight_toRightOf="parent" /> 


    <TextView 
     android:id="@+id/tv_notification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Enable Notification" 
     app:layout_constraintTop_toBottomOf="@+id/tv_stay_awake"/> 

    <Switch 
     android:id="@+id/switch_notification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/switch_stay_awake"/> 


    <TextView 
     android:id="@+id/tv_location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Enable location" 
     app:layout_constraintTop_toBottomOf="@+id/tv_notification"/> 
    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/switch_notification"/> 

</android.support.constraint.ConstraintLayout> 

과는 다음과 같이 밝혀 : 당신이 볼 수 있듯이 enter image description here

, 스위치 버튼은 텍스트 뷰에 정렬되지 않습니다. 같은 라인 레벨의 TextView와 어떻게 정렬시킬 수 있습니까? LinearyLayout과 RelativeLayout을 사용했을 때와 똑같이 보일 것입니다. TextView 및 Switch 단추를 각 줄마다 RelativeLayout에 넣을 수는 있지만, 그렇게하면 ConstrainLayout을 사용할 이유가 없습니다.

답변

4

시도해 볼 수 있습니다.

Switch XML 코드에 app:layout_constraintBaseline_toBaselineOf="@+id/tv_notification"을 추가하십시오.

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

    <TextView 
     android:id="@+id/tv_stay_awake" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Stay awake"/> 

    <Switch 
     android:id="@+id/switch_stay_awake" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintBaseline_toBaselineOf="@+id/tv_stay_awake" 
     app:layout_constraintRight_toRightOf="parent"/> 


    <TextView 
     android:id="@+id/tv_notification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Enable Notification" 
     app:layout_constraintTop_toBottomOf="@+id/tv_stay_awake"/> 

    <Switch 
     android:id="@+id/switch_notification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintBaseline_toBaselineOf="@+id/tv_notification" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/switch_stay_awake"/> 


    <TextView 
     android:id="@+id/tv_location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="Enable location" 
     app:layout_constraintTop_toBottomOf="@+id/tv_notification"/> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintBaseline_toBaselineOf="@+id/tv_location" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/switch_notification"/> 

</android.support.constraint.ConstraintLayout> 

enter image description here

+0

이 지금은 각 설정 사이의 구분선을 추가 할 OUTPUT, 새로운 전망을 도입하지 않고 그것을 할 수있는 방법은 무엇입니까? –

+0

나는 길을 가지지 않았다. 내가 이것을하고 싶다면, 새로운 'View'를 줄 구분자로 추가 할 것이다. – KeLiuyue