教材:第一行代码(第2版)
当系统自带的控件不能满足需求时,
我们也可以利用上面的继承结构来创建自定义控件。
新建项目UICustomViews
创建一个自定义的标题栏
一般程序中可能很多活动都需要一个这样的标题栏,
但在每个活动的布局中都编写同样的标题栏代码会导致重复,
所以可以引入布局。
//创建一个布局title.xml
<?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:background="@drawable/title_bg">
<Button
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/back"
android:text="Back"
android:textColor="#8B3626" />
<TextView
android:id="@+id/title_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="Title text"
android:gravity="center"
android:textColor="#8B3626"
android:textSize="24sp" />
<Button
android:id="@+id/title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@drawable/edit"
android:text="Edit"
android:textColor="#8B3626" />
</LinearLayout>
//activity_main.xml
<?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="match_parent"
>
<include layout="@layout/title" /> //添加标题栏
</LinearLayout>
//MainActivity
//隐藏系统自带标题栏
package com.example.uicustomviews;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
//自动生成为import android.app.ActionBar; 报错
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionbar = getSupportActionBar();
if(actionbar != null)
actionbar.hide();
}
}
为功能相同的控件(比如:back-返回功能)在每个活动中单独编写一次事件注册代码,
也会导致代码重复,
所以可以使用自定义控件。
//新建src/main/java/com.example.uicustomviews/TitleLayout.java类
//TitleLayout继承自LinearLayout
//TitleLayout.java
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs){
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title,this);
}
}
//修改activity_main.xml
<?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="match_parent"
>
<com.example.uicustomviews.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//TitleLayout.java中添加按钮事件
Button titleBack = (Button) findViewById(R.id.title_back);
Button titleEdit = (Button) findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"You clicked Edit button",Toast.LENGTH_SHORT).show();
}
});