本文实例讲述了Android编程实现自定义控件的方法。分享给大家供大家参考,具体如下:
很多时候Android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。
首先定义一个layout实现按钮内部布局:
<?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="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dip" android:paddingLeft="40dip" android:paddingTop="5dip" android:src="@drawable/right_icon" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dip" android:text="确定" android:textColor="#000000" /> </LinearLayout>
接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。
public class ImageBtn extends LinearLayout { private ImageView imageView; private TextView textView; public ImageBtn(Context context) { super(context); // TODO Auto-generated constructor stub } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.imagebtn, this); imageView=(ImageView) findViewById(R.id.imageView1); textView=(TextView)findViewById(R.id.textView1); } /** * 设置图片资源 */ public void setImageResource(int resId) { imageView.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text) { textView.setText(text); } }
在需要使用这个自定义控件的layout中加入这控件,只需要在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="horizontal" > <cn.com.karl.view.ImageBtn android:id="@+id/btn_right" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/btn" /> <cn.com.karl.view.ImageBtn android:id="@+id/btn_error" android:layout_marginLeft="5dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/btn" /> </LinearLayout>
这里用到了背景图片 在drawable/btn.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> <item android:state_pressed="true" android:drawable="@drawable/btn_white"></item> <item android:state_checked="true" android:drawable="@drawable/btn_white"></item> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> </selector>
最后在activity中设置该控件,和其他控件差不多:
public class IdentifyButtonActivity extends Activity { private ImageBtn imageBtn1; private ImageBtn imageBtn2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.identifybutton); imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right); imageBtn2=(ImageBtn) this.findViewById(R.id.btn_error); imageBtn1.setTextViewText("确定"); imageBtn2.setTextViewText("取消"); imageBtn1.setImageResource(R.drawable.right_icon); imageBtn2.setImageResource(R.drawable.error_icon); imageBtn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show(); } }); imageBtn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show(); } }); } }
最后看看我们自定义控件的效果吧!
点击后还有按下按钮的效果。
希望本文所述对大家Android程序设计有所帮助。
本文向大家介绍Android编程实现自定义Dialog的大小自动控制方法示例,包括了Android编程实现自定义Dialog的大小自动控制方法示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程实现自定义Dialog的大小自动控制方法。分享给大家供大家参考,具体如下: Android应用开发中,无论是出于功能还是增加用户体验,弹出对话框(Dialog)进行一些操作提示是非
本文向大家介绍Android 自定义底部上拉控件的实现方法,包括了Android 自定义底部上拉控件的实现方法的使用技巧和注意事项,需要的朋友参考一下 前言 又到了新的一月,今天提供一个Android自定义底部上拉布局的实现,起因是自己在项目中需要实现这样一个控件,干脆自己写一个练练手。 写完了觉得能想到的需求都基本有了(可能会有其它需求,不过基本上改吧改吧就行了),又花了一点时间直接放到了Git
本文向大家介绍Android实现自定义轮播图片控件示例,包括了Android实现自定义轮播图片控件示例的使用技巧和注意事项,需要的朋友参考一下 要完成一个轮播图片,首先想到的应该是使用ViewPager来实现。ViewPager已经有了滑动的功能,我们只要让它自己滚动。再加上下方的小圆点就行了。所以我们本次的自定义控件就是由ViewPager和LinearLayout叠加起来组成的。 直接先上效果
本文向大家介绍android仿Adapter实现自定义PagerAdapter方法示例,包括了android仿Adapter实现自定义PagerAdapter方法示例的使用技巧和注意事项,需要的朋友参考一下 PagerAdapter简介 PagerAdapter是android.support.v4包中的类,它的子类有FragmentPagerAdapter, FragmentStatePager
本文向大家介绍Android编程实现自定义进度条颜色的方法,包括了Android编程实现自定义进度条颜色的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程实现自定义进度条颜色的方法。分享给大家供大家参考,具体如下: android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程! 这个没法了只能看源
本文向大家介绍C#编程实现自定义热键的方法,包括了C#编程实现自定义热键的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#编程实现自定义热键的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的C#程序设计有所帮助。
本文向大家介绍Android自定义控件(实现状态提示图表),包括了Android自定义控件(实现状态提示图表)的使用技巧和注意事项,需要的朋友参考一下 前面分析那么多系统源码了,也该暂停下来休息一下,趁昨晚闲着看见一个有意思的需求就操练一下分析源码后的实例演练—-自定义控件。 这个实例很适合新手入门自定义控件。先看下效果图: 横屏模式如下: 竖屏模式如下: 看见没有,这个控件完全自定义的,连文字等
本文向大家介绍Android自定义控件实现折线图,包括了Android自定义控件实现折线图的使用技巧和注意事项,需要的朋友参考一下 本文实例实现一个如下图所示的Android折线图,供大家参考,具体内容如下 首先是控件绘图区域的划分,控件左边取一小部分(控件总宽度的八分之一)绘制表头,右边剩余的部分绘制表格 确定表格的行列数,首先绘制一个三行八列的网格,设置好行列的坐标后开始绘制 网格绘制完成后,