ConstraintLayout의 MATCHPARENT를 사용할 때 예상치 못한 행동

이 보도에 관하여


이 글은 아래의 사람들에게 도움이 된다.그러니까 내가
-ConstraintLayout 사용
- ConstraintLayout에서 matchparent 사용
ConstraintLayout의 MATCHPARENT를 사용할 때 예상치 못한 행동을 소개함으로써 MATCH를 잘 진행하도록 하겠습니다.CONSTRAINT를 사용할 자계를 담은 기사다.

ConstraintLayout의 MATCHPARENT 추천하지 않음


원래 ConstraintLayout의 MATCH였어요.PARENT는 추천하지 않습니다ConstraintLayout의 문서에 따르면 ConstraintLayout,layout-width 및 layoutheight에서 다음 중 하나를 지정합니다.
- 고정 치수(120dp 등)
- WRAP_CONTENT(wrap_content)
- MATCH_CONSTRAINT(실제 레이아웃 파일에 0dp 지정)
그리고 MATCHPARENT 지정을 권장하지 않습니다. (대신 MATCH CONSTRAINT는 상하좌우의 제약을 Parent에 설정합니다.)
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

못 쓰는 건 아니에요.


ConstraintLayout 내 MATCHPARENT를 사용하더라도 보통 예상대로 움직인다.
예를 들어, 다음과 같은 레이아웃 파일을 만들었다고 가정합니다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.com.constraintlayoutbadexample.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!" />

</android.support.constraint.ConstraintLayout>
이제 TextView가 화면에 가득 표시됩니다.

MATCH_PARENT를 사용할 때 예상치 못한 행동


잠시 움직이기 때문에 저는 MATCH입니다.PARENT가 추천하지 않는 것에 별로 신경 쓰지 않아서 저도 모르게 썼어요.단, 다음은 MATCH RecyclearView입니다.PARENT 구성 지정 시 예상치 못한 사태가 발생했습니다.(MainActivity 클래스의 코드는 글의 마지막에 보충됩니다)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.com.constraintlayoutbadexample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_bright" />

</android.support.constraint.ConstraintLayout>
item.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="56dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="16dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/imageView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Item" />

</android.support.constraint.ConstraintLayout>
이 레이아웃을 사용하여 목록을 표시할 때는 다음과 같습니다.

ImageView 옆에 있어야 하는 TextView가 표시되지 않습니다.

MATCH_CONSTRAINT 지정을 통한 해결


잠시 푹 빠진 후, MATCHCONSTRAINT를 지정하여 제약을 parent에 설정하여 TextView를 안전하게 표시할 수 있습니다.
activity_main.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="example.com.constraintlayoutbadexample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

결론


ConstraintLayout의 MATCHPARENT 대신 MATCH 사용CONSTRAINT를 사용하십시오.

보충: MainActivity 클래스 코드


MainActivity.kt
package example.com.constraintlayoutbadexample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = ItemAdapter(arrayListOf("hoge", "fuga", "piyo"))
    }
}

private class ItemAdapter(private val items: List<String>) : RecyclerView.Adapter<ItemViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
        val view = LayoutInflater.from(parent?.context)
                .inflate(R.layout.item, parent, false)
        return ItemViewHolder(view)
    }

    override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
        holder?.textView?.text = items[position]
    }

    override fun getItemCount(): Int = items.size
}

private class ItemViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
    val textView = itemView?.findViewById<TextView>(R.id.textView)
}

좋은 웹페이지 즐겨찾기