Android备忘录 -- 自定义控件

本文介绍了在Android开发中如何利用引入布局和自定义控件来提高UI开发效率,避免重复代码,实现统一风格的界面设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.引入布局

    当有一些重复出现的控件配置(如统一的标题栏)出现时,为了避免代码重复,就需要使用  引入布局  了。

    下面以标题栏为例子说明

    1.1 新建布局title.xml             

<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="5dip"
        android:background="@drawable/back_bg"
        android:text="Back"
        android:textColor="#fff" />
    <TextView
        android:id="@+id/title_text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Title Text"
        android:textColor="#fff"
        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="5dip"
        android:background="@drawable/edit_bg"
        android:text="Edit"
        android:textColor="#fff" />
</LinearLayout>
    1.2 引入title.xml,即标题栏

<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>
        如上面代码所示,只需要一行 include 语句就可将标题栏布局引入了。

    1.3 隐藏系统自带标题栏

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}
    程序运行效果图如下所示

引入标题栏
2. 创建自定义控件

    引入布局后,经常还需要为布局的控件注册响应时间,这时也是为了避免代码重复,需要用到  自定义控件  。

    下面仍以自定义标题栏为例。

    1. 新建自定义控件 TitleLayout类继承自LinearLayout,代码如下

public class TitleLayout extends LinearLayout{
    public TitleLayout(Context context, AttributeSet attrs){
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);
    }
}

        首先重写了LinearLayout中的带有2个参数的构造函数,在布局中引入了TitleLayout这个自定义控件就会调用这个构造函数了。然后在构造函数中对标题栏布局进行动态加载。

        上面代码中,通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件。inflate()方法接收两个参数,第一个参数是要加载的布局文件的ID,第二个参数是加载文件的父级布局。

    2. 在活动布局文件中添加自定义控件

        修改activity_main.xml文件中的代码,如下

<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"
    ></com.example.uicustomviews.TitleLayout>
</LinearLayout>
        如代码所示, 添加自定义控件和添加普通控件的方式基本是一样的,只是添加自定义控件时需要指明控件的完整类名。

    3.为标题栏的按钮注册点击事件

        具体示例代码如下

public class TitleLayout extends LinearLayout{
    public TitleLayout(Context context, Attributes attrs){
        super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.title, this);
		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 v){
				((Activity)getContext()).finish();
			}
		});
		titleEdit.setOnclickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
			}
		});
	}
}
        程序效果图如下

自定义标题栏-输出




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值