0%
TitleLayout
示例(Kotlin)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class TitleLayout(context: Context,attrs: AttributeSet) : LinearLayout(context,attrs){
init { LayoutInflater.from(context).inflate(R.layout.title,this)
btnTitleBack.setOnClickListener{ val activity = context as Activity activity.finish() }
btnTitleEdit.setOnClickListener{ Toast.makeText(context,"Toast",Toast.LENGTH_SHORT).show() } } }
|
title.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btnTitleBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Back" />
<TextView android:id="@+id/tvTitleText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Title" android:gravity="center"/> <Button android:id="@+id/btnTitleEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Edit" /> </LinearLayout>
|
activity_main.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".activity.MainActivity" android:orientation="vertical">
<!-- 使用自定义控件 --> <com.example.myapplication.view.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
|