自定义控件实现

目标:自定义控件实现

步骤:

1)准备控件Layout,e.g.

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/holo_blue_light"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10.0dip"
        android:background="@drawable/ic_launcher"
        android:text="返回"
        android:textColor="@android:color/white" />
  
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:singleLine="true"
        android:text="标题"
        android:textColor="@android:color/white" />
        
</LinearLayout>

2)准备自定义控件对应类,通常需要继承Layout类,实现其Constructor,注入刚才的layout 配置文件,e.g.

NavigationHeader.java

public class NavigationHeader extends LinearLayout {
	
	private Activity activity;
	private Button btn_left;
    private TextView tv_title;
    
	public NavigationHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater layoutInflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        layoutInflater.inflate(R.layout.navigation_header, this);
...

3)在XML中调用控件,e.g.

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.testApp5.NavigationHeader
        android:id="@+id/uiNavigationHeader"
        android:layout_width="fill_parent" 
        android:layout_height="45dip"
        />
    
    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="result content"
        />
        
</LinearLayout>

最基本的就是这些,更多的如自定义参数 可以参考以下blog:


Reference - http://blog.youkuaiyun.com/worker90/article/details/7909328


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值