Android如何实现自定义控件

学习Android也有一个月的时间了,一直不知道怎么能够实现自定义的 控件!今天学习了下,做了一个简单的demo!

我想要实现如下的效果:


首先:我们要定义要按钮内部的布局结构  custom_btn.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    > 
 <ImageView 
  android:id="@+id/iv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical"
     android:paddingLeft="10.0dip"
     android:paddingTop="10.0dip"
     android:paddingBottom="10.0dip"
     /> 
 <TextView 
  android:id="@+id/tv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:layout_marginLeft="8dip" 
     android:layout_gravity="center_vertical" 
     android:paddingLeft="5.0dip"
     android:paddingTop="10.0dip"
     android:paddingBottom="10.0dip"
     android:paddingRight="10.0dip"
     android:textSize="18.0sp"
     /> 
</LinearLayout>

其次:我们需要写一个类来集成 LinearLayout,导入刚刚定义的按钮布局,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:

CustomButton.java

package com.example.zuheui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomButton extends LinearLayout {
	private ImageView iv;
	private TextView tv;
	public CustomButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		//导入布局
		LayoutInflater.from(context).inflate(R.layout.custom_btn, this, true);
		iv=(ImageView) findViewById(R.id.iv);
		tv=(TextView) findViewById(R.id.tv);
	}
	/*
	 * 设置图片资源
	 */
	public void setImageResource(int resId){
		iv.setImageResource(resId);
	}
	/*
	 * 设置显示文字
	 */
	
	void setTextViewText(String text) {
		// TODO Auto-generated method stub
		tv.setText(text);
	}

}

再次,在需要使用这个自定义控件的layout中加入这控件,只需要在activity_main.xml中加入即可。方法如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 > 
 <com.example.zuheui.CustomButton
     android:id="@+id/bt_confirm" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:background="@drawable/button_bg" 
     /> 
 <com.example.zuheui.CustomButton
     android:id="@+id/bt_cancel" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:background="@drawable/button_bg" 
    /> 
</LinearLayout>
最后一步,即在activity中设置该控件的内容

MainActivity.java

package com.example.zuheui;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	private CustomButton bt_confirm;
	private CustomButton btn_cancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		bt_confirm=(CustomButton) findViewById(R.id.bt_confirm);
		btn_cancel=(CustomButton) findViewById(R.id.bt_cancel);
		bt_confirm.setTextViewText("确定");
		bt_confirm.setImageResource(R.drawable.ic_launcher);
		
		btn_cancel.setTextViewText("取消");
		btn_cancel.setImageResource(R.drawable.ic_launcher);
		
		bt_confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
				
			}
		});
		btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

@author www.lelexie.com  

本文出自:恋人SEO---http://www.loverseo.com





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值