Share
Embed
レイアウトを使い回す 〜ConstraintLayout編〜
複数の画面で、共通のレイアウトを使いたいことがあると思います。このようなときは、予め共通で使用するレイアウトリソースを定義しておき、各画面でさせると、作業の効率化・デザインの変化に強くすることができます。
今回は、ConstraintLayoutでレイアウトを使い回す方法について解説しました。使い古されたネタなので、目新しさはありませんが、改めてまとめてみました。
sussan0416
September 17, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
20 slides
-
ڞ௨Խ͢ΔϨΠΞτΛఆٛ <?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="wrap_content" android:orientation="vertical"> <TextView
android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title TextView" android:textColor="@android:color/primary_text_light" android:textSize="24sp" /> <TextView android:id="@+id/subtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Subtitle TextView" /> </LinearLayout> -
ڞ௨Խ͢ΔϨΠΞτΛఆٛ <?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="wrap_content" android:orientation="vertical"> <TextView
android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title TextView" android:textColor="@android:color/primary_text_light" android:textSize="24sp" /> <TextView android:id="@+id/subtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Subtitle TextView" /> </LinearLayout> NFSHFʹͤͣɺ7JFX(SPVQʹͨ͠ $POTUSBJOU-BZPVUͰ0, -
ڞ௨Խ͢ΔϨΠΞτΛఆٛ <?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="wrap_content" android:orientation="vertical"> <TextView
android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title TextView" android:textColor="@android:color/primary_text_light" android:textSize="24sp" /> <TextView android:id="@+id/subtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Subtitle TextView" /> </LinearLayout> JODMVEFͨ͠ઌͰPWFSSJEF͞ΕΔɻ JODMVEFઌʹ߹ΘͤΔ΄͏͕࡞ۀָ͕ -
JODMVEFͰϨΠΞτΛಡΈࠐΉ$POTUSBJOUΛదʹઃఆ͢Δ <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:text="Header TextView" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/include_1" layout="@layout/layout_included" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <!-- include͢Δ͚ͩ܁Γฦ͢ --> </androidx.constraintlayout.widget.ConstraintLayout> -
JODMVEFͰϨΠΞτΛಡΈࠐΉ$POTUSBJOUΛదʹઃఆ͢Δ <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:text="Header TextView" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/include_1" layout="@layout/layout_included" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <!-- include͢Δ͚ͩ܁Γฦ͢ --> </androidx.constraintlayout.widget.ConstraintLayout> JODMVEFλάΛઃஔ͠ɺ MBZPVUଐੑͰɺಡΈࠐΉϨΠΞτΛࢦఆ͢Δ -
JODMVEFͰϨΠΞτΛಡΈࠐΉ$POTUSBJOUΛదʹઃఆ͢Δ <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:text="Header TextView" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/include_1" layout="@layout/layout_included" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <!-- include͢Δ͚ͩ܁Γฦ͢ --> </androidx.constraintlayout.widget.ConstraintLayout> MBZPVU@ ଐੑΛ͏ͨΊɺ MBZPVU@XJEUI MBZPVU@IFJHIUΛΦʔόʔϥΠυ -
JODMVEFͰϨΠΞτΛಡΈࠐΉ$POTUSBJOUΛదʹઃఆ͢Δ <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:text="Header TextView" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/include_1" layout="@layout/layout_included" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <!-- include͢Δ͚ͩ܁Γฦ͢ --> </androidx.constraintlayout.widget.ConstraintLayout> ͋ͱීஈͲ͓Γͷ$POTUSBJOUΛ͚ͭΔ -
JODMVEFͰϨΠΞτΛಡΈࠐΉ$POTUSBJOUΛదʹઃఆ͢Δ <?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" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="20dp" android:layout_marginEnd="16dp" android:text="Header TextView" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <include android:id="@+id/include_1" layout="@layout/layout_included" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" app:layout_constraintEnd_toEndOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <!-- include͢Δ͚ͩ܁Γฦ͢ --> </androidx.constraintlayout.widget.ConstraintLayout> ϓϩάϥϜ͔Βѻ͏ʹɺJEΛ͚͓ͭͯ͘ -
7JFXΛϓϩάϥϜ͔Βѻ͏ʹ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // findViewByIdͰɺViewGroupͷIDΛࢦఆ͠ɺ͞Βʹ͏ҰfindViewByIdΛॏͶΕɺViewΛऔಘͰ͖Δ val title1 = findViewById<ViewGroup>(R.id.include_1).findViewById<TextView>(R.id.title) title1.text = "The first title" // ViewGroupΛࢦఆ͠ͳ͔ͬͨ߹ɺ࠷ॳʹग़ݱ͢ΔView͕ରʹͳΔ val subtitle1 = findViewById<TextView>(R.id.subtitle) subtitle1.text = "The first subtitle" // Kotlin Android Extensions include_2.title.text = "Second title" include_2.subtitle.text = "Second subtitle" include_3.title.text = "Third title" include_3.subtitle.text = "Third subtitle" } } pOE7JFX#Z*EΛݺͿʢJODMVEFॴͷJEͱɺϓϩάϥϜ͔Βѻ͏7JFXͷJEʣ "OESPJE,59Λ༻͢ΔͱɺΊͬͪΌָ -
·ͱΊ Γग़ͨ͠ϨΠΞτͷ9.- ಡΈࠐΉํͷ9.- w ಡΈࠐΉઌ͕$POTUSBJOU-BZPVU ͷ߹NFSHFͰͳ͘ɺ 7JFX(SPVQʹ͢Δ w MBZPVU@XJEUI MBZPVU@IFJHIU
ɺJODMVEFઌͱ߹Θ͓ͤͯ͘ͱ ࡞ۀָ͕ w JODMVEFͱMBZPVUଐੑͰɺଞͷ ϨΠΞτΛಡΈࠐΊΔ w MBZPVU@ ଐੑ͕ඞཁͳͷͰɺ MBZPVU@XJEUI MBZPVU@IFJHIUΛ ΦʔόʔϥΠυ͢Δ w %FTJHOϏϡʔ1BMFUUF͕͍ͩͨ ͍ͬͯ͘ΕΔ