3.1 布局类型
- LinearLayout、RelativeLayout(旧)、ConstraintLayout(推荐)。
3.2 XML 布局示例
<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/tvHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.3 RecyclerView 列表
class MyAdapter(private val items: List<String>) : RecyclerView.Adapter<MyVH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
val view = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false)
return MyVH(view)
}
override fun onBindViewHolder(holder: MyVH, position: Int) {
holder.text.text = items[position]
}
override fun getItemCount() = items.size
}
class MyVH(view: View): RecyclerView.ViewHolder(view) {
val text: TextView = view.findViewById(android.R.id.text1)
}