Android学习笔记三—自定义控件

自定义控件,以标题栏为例。
1.创建布局title.xml
当每个或多个Activity的布局,需要使用同一个布局时,如标题栏布局,可以将该布局单独建立成一个布局文件如title.xml。

<?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/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="Back"
        android:textColor="#fff"/>
    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TitleText"

        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:text="Edit"
        android:textColor="#fff"/>

</LinearLayout>

2.在MainActivity中将原来的标题栏隐藏掉,如下

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏原有标题栏
        ActionBar actionbar=getSupportActionBar();
        if(actionbar!=null){
            actionbar.hide();
        }
    }

3.创建自定义控件
创建标题栏控件,标题栏控件继承自LinearLayout类。
第一步,首先,在com.xx.xx包下创建TitleLayout类,该类继承LinearLayout类。该类重写了LinearLayout类的构造函数,布局中引入该控件就会调用该构造函数。然后,在构造方法中给返回键Back和编辑键Edit注册监听器。该类有点类似于Activity,Activity加载布局是在OnCreate()方法中,该类加载布局是在构造方法中。之后就是实例化按钮等控件,并注册监听器。

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet 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(),"点击了编辑按钮",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

第二步,在Activity中,如MainActivity引入该控件,注意,要指明控件的完成类名,如下:

 <com.xx.xx.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值