android 레이아웃 의 ConstraintLayout 사용

사실 ConstraintLayout 는 Android Studio 2.2 의 주요 추가 기능 중 하나 이자 Google 이 2016 년 I/O 대회 에서 중점적으로 홍보 한 기능 입 니 다.안 드 로 이 드 시각 화 편집 을 위 한 노력 입 니 다.android studio 의 시각 화 편집 은 개인 적 으로 추천 하지 않 지만 ConstraintLayout 레이아웃 의 사용 은 알 필요 가 있 습 니 다.
1.ConstraintLayout 를 사용 하려 면 app 의 build.gradle 에 도입 해 야 합 니 다.

compile 'com.android.support.constraint:constraint-layout:1.0.2'
2.먼저 간단 한 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="match_parent"
tools:context="com.app.qichun.hellowrod.MainActivity">

<TextView
  android:id="@+id/tv1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello World!"
  app:layout_constraintTop_toTopOf="parent"
 />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="     "
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="     "
  app:layout_constraintBottom_toBottomOf="parent"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="     "
  app:layout_constraintLeft_toRightOf="@+id/tv1"
  android:layout_marginLeft="10dp"
  />
 </android.support.constraint.ConstraintLayout>


시청자 여러분 은 xml 에서 네 개의 간단 한 Textview 분포 위 치 를 알 수 있 습 니 다.id 를 tv 의 첫 번 째 컨트롤 을 기준 으로 두 번 째 컨트롤 은 첫 번 째 컨트롤 의 아래 에 있 고 전체 레이아웃 의 왼쪽 에 있 습 니 다.세 번 째 컨트롤 은 전체 부모 레이아웃 의 왼쪽 아래 에 있 습 니 다.네 번 째 컨트롤 은 첫 번 째 컨트롤 의 오른쪽 에 있 습 니 다.
자세히 살 펴 보면 모든 Textview 는 비슷 한 속성 을 가지 고 있 습 니 다.
예 를 들 어 첫 번 째 컨트롤 의:

app:layout_constraintTop_toTopOf="parent"
두 번 째 컨트롤의

app:layout_constraintTop_toBottomOf="@+id/tv1"
글자 그대로:
이 컨트롤 의 한 변 과 한 컨트롤 의 한 변 을 정렬 합 니 다.
예 를 들 어 첫 번 째 컨트롤 은 이 컨트롤 의 윗부분 과 부모 구조의 윗부분 을 정렬 하면 첫 번 째 컨트롤 이 왼쪽 위 에 있 습 니 다.두 번 째 컨트롤 의 상단 과 첫 번 째 컨트롤 즉 id=tv1 컨트롤 의 아래쪽 이 정렬 되 어 있 으 며,자 연 스 럽 게 두 번 째 컨트롤 은 첫 번 째 컨트롤 의 바로 아래 에 있 습 니 다.다른 것 은 하 나 를 보면 열 을 안다.
지금 우 리 는 하나의 구 조 를 다시 시험 하여 가장 흔히 볼 수 있 는 구 조 를 만 들 도록 규정 하고 있다.

코드 는 다음 과 같 습 니 다:

<?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"
tools:context="com.app.qichun.hellowrod.MainActivity">

<TextView
  android:id="@+id/tv1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="  "
 <!--                 !-->
  app:layout_constraintTop_toTopOf="parent"
 />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="   "
  <!--                 !-->
  app:layout_constraintRight_toLeftOf="parent"
  android:layout_marginRight="10dp"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="  "
 <!--        tv1      !-->
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="    "
  <!--                 !-->
  app:layout_constraintRight_toLeftOf="parent"
 <!--        tv1      !-->
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  android:layout_marginRight="10dp"
  />
</android.support.constraint.ConstraintLayout>


이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기