공통으로 사용하게 될 xml을 만들었습니다. 이걸 a_xml이라고 하겠습니다.
이 a_xml을 include 를 사용해서 불러와서 사용하는데요. 문제는 a_xml안에 textview가 있고 거기에 데이터 바인딩으로 값을 대입하려고 합니다. 말로 하려니 어려워서 소스 첨부 합니다.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="txtTitle"
type="java.lang.String" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:background="@color/black"
android:layout_width="40dp"
android:layout_height="40dp"
android:textColor="@color/white"
android:text="@{txtTitle}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
이 소스가 a_xml 입니다. 저기 보면 상단에 txtTitle이라고 변수를 string 형으로 선언하고 그걸 textView에 Text에 연결하였습니다.
그리고 이 xml을 include하는 곳을 보면요
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<include
android:id="@+id/iddd"
layout="@layout/activity_include"
app:txtTitle='@{"닉네임 변경"}'
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
include로 불러오면서 app:txtTitle='@{"닉네임 변경"}' 이라고 값을 넣었습니다. 하지만 앱을 실행하면 글자가 나타나지 않습니다. 도대체 뭐가 문제인지 모르겠습니다. 부디 해결법을 알려주시면 감사하겠습니다.
그리고 해결 방법이 있다면 text 말고 drawable로 된 이미지도 하고 싶은데 아이디어 있으면 알려주시면 감사하겠습니다.