有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果。
以下主要参考《第一行代码》
1.自定义一个标题栏:
系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在onCreate()中调用requestWindowFeature(Window.FEATURE_NO_TITLE);设置不显示标题栏。
下面自定义一个标题栏,中间显示标题,左右各有一个按钮:
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:orientation="horizontal" android:background="#bbbbbb" > <Button android:id="@+id/btn_back" android:text="@string/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_gravity="left|center_vertical" android:textColor="#0099cc" android:layout_weight="1"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#0099cc" android:text="@string/this_is_title" android:layout_gravity="center" android:gravity="center" android:layout_weight="2"/> <Button android:id="@+id/btn_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/edit" android:layout_margin="5dp" android:layout_gravity="right|center_vertical" android:textColor="#0099cc" android:layout_weight="1"/> </LinearLayout>
Activity代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.title); }
运行结果:
(⊙o⊙)…有点丑哈,不过仔细看,还是有点像标题栏的。
2.复用布局代码:
想让这个标题栏应用在以后的每个布局文件,要怎么做呢?
总不能每次都把这些xml代码重写一遍吧。
android布局中提供了类似于c预处理指令#include的<include>标签,可以实现布局代码的复用。
下面新建一个first_layout.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="vertical" > <include layout="@layout/title"/> <Button android:id="@+id/btn" android:text="@string/i_m_a_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
修改setContentView(R.layout.first_layout);
显示结果:
现在Back和Edit按钮都没有任何事件处理的,怎样实现点击Back按钮就结束当前Activity呢?方法跟之前的做法完全一样,使用findViewById()根据id找到Back按钮,然后设置click事件监听即可。
代码如下:
public class FirstActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_layout); Button btn = (Button) findViewById(R.id.btn_back); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FirstActivity.this.finish(); } }); } }
布局文件的复用已然通过<include>实现了,但是每次都要重新写事件监听,还是觉得麻烦……到这里一般就会想到抽象出一个自定义类,每次需要的时候,直接使用该自定义类不就行了,其实就是自定义控件的做法了。
3.自定义控件,复用功能代码
TitleLinearLayout.java代码:
public class TitleLinearLayout extends LinearLayout { public TitleLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); Button btn_back = (Button) findViewById(R.id.btn_back); btn_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("clicked","back"); ((Activity)getContext()).finish(); } }); } }
继承自LinearLayout,实现带两个参数的构造方法。在构造方法中,加载布局文件,并对其中的Back按钮进行事件监听设置。
LayoutInflater.from(context).inflate(R.layout.title, this);用于动态加载布局文件。
注意到,Activity中有一个获取LayoutInflater的方法,所以,也可以使用下面一行代码加载布局文件:
((Activity)context).getLayoutInflater().inflate(R.layout.title, this);这种方法,在Activity代码中比较常用,而这里需要进行类型强转,反倒麻烦点,而且不如第一个方法安全。
如何使用自定义的控件呢?
first_layout代码如下:
<?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="vertical" > <cn.csc.custom_ui.TitleLinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> </cn.csc.custom_ui.TitleLinearLayout> <Button android:id="@+id/btn" android:text="@string/i_m_a_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
说明:
1)在布局文件中,引用自定义的控件,需要使用完整的类限定名,即包名.类名的方式;
2)在定义控件中,设置属性时,使用alt+/进行代码提示补全功能将经常不可用,标签名可以先设置为内置控件,然后进行属性的设置,之后再把标签名改回到自定义的控件的完整限定名即可。