自定义android控件

来源:互联网 发布:顺序循环队列c语言 编辑:程序博客网 时间:2024/06/04 22:09
package com.example.ui;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.Toast;import com.example.uiwidgetcustom.R;public class HeaderLayout extends RelativeLayout {    private Button btn_back;        public HeaderLayout(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        LayoutInflater.from(context).inflate(R.layout.headerlayout, this);            initView();        initEvent();    }    public void initView(){                btn_back = (Button) findViewById(R.id.back);        }        public void initEvent(){                btn_back.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                Toast.makeText(getContext(), "返回",1).show();            }        });            }}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/headerlayout"    android:background="#00A779" >        <Button        android:id="@+id/back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回"        android:textColor="#fff"        android:background="#00A779"></Button>        <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:textColor="#fff"        android:text="Android4高级编程"        ></TextView></RelativeLayout>


然后引入:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:background="#fff" >    <com.example.ui.HeaderLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        ></com.example.ui.HeaderLayout></LinearLayout>




0 0