Android 自定义控件
本文参考书籍:郭霖《第一行代码-Android》。
关键字:自定义控件
需求
对于多个Activity,在界面顶部使用一个统一的标题栏,标题栏上:返回按钮-标题文字-编辑按钮,点击可响应操作
简单分析
把这个标题栏做成一个控件,在每个Activity中使用
- xml定义标题栏样式
- 创建自己的控件类class
- 在主Activity的xml文件中使用自定义的控件
XML和代码文件
title_all.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"
android:orientation="horizontal" >
<Button
android:id="@+id/title_btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="this is the title" />
<Button
android:id="@+id/title_btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="edit" />
</LinearLayout>
MyTitleLayout.java
, 创建自己的控件类,包含样式和事件响应:
package com.example.androidcustomedviewtest;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.myising99.androidcustomedview.R;
public class MyTitleLayout extends LinearLayout
{
public MyTitleLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_all, this); // 填充布局
Button btnBack = (Button) findViewById(R.id.title_btn_back);
Button btnEdit = (Button) findViewById(R.id.title_btn_edit);
// 注册事件
btnBack.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
((Activity) getContext()).finish();
}
});
btnEdit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getContext(), "clicked edit btn", Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
, 在主Activity的xml文件中使用自定义控件:
<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"
android:orientation="vertical"
tools:context="com.example.androidcustomedviewtest.MainActivity" >
<!-- 自定义控件 com.example.androidcustomedview.MyTitleLayout, 需要指明控件的完整名 -->
<com.example.androidcustomedviewtest.MyTitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.androidcustomedviewtest.MyTitleLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/hello_world" />
</LinearLayout>
MainActivity.java
, 主Activity:
package com.example.androidcustomedviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import com.myising99.androidcustomedview.R;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 先去掉系统默认title bar
setContentView(R.layout.activity_main);
}
}
当主Activity运行时,界面顶部就显示自定义的控件。