android 레이아웃 의 ConstraintLayout 사용
5140 단어 androidConstraintLayout
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>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.