【代码】android自定义控件实例

发布时间:2013-8-15 16:25    发布者:reggae
关键词: android
很多时候android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。
(PS:新建的QQ群,有兴趣可以加入一起讨论:Android学习交流群278744577,验证:eec
首先定义一个layout实现按钮内部布局:
源码打印?
android自定义控件实例.jpg
  1. android:layout_width="fill_parent"
  2. android:layout_height="fill_parent"
  3. android:orientation="horizontal" >
  4. android:id="@+id/imageView1"
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:layout_gravity="center_vertical"
  8. android:paddingBottom="5dip"
  9. android:paddingLeft="40dip"
  10. android:paddingTop="5dip"
  11. android:src="@drawable/right_icon" />
  12. android:id="@+id/textView1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center_vertical"
  16. android:layout_marginLeft="8dip"
  17. android:text="确定"
  18. android:textColor="#000000" />
复制代码


接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。
源码打印?
  1. public class ImageBtn extends LinearLayout {
  2. private ImageView imageView;
  3. private TextView textView;
  4. public ImageBtn(Context context) {
  5. super(context);
  6. // TODO Auto-generated constructor stub
  7. }
  8. public ImageBtn(Context context, AttributeSet attrs) {
  9. super(context, attrs);
  10. // TODO Auto-generated constructor stub
  11. LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  12. inflater.inflate(R.layout.imagebtn, this);
  13. imageView=(ImageView) findViewById(R.id.imageView1);
  14. textView=(TextView)findViewById(R.id.textView1);
  15. }
  16. /**
  17. * 设置图片资源
  18. */
  19. public void setImageResource(int resId) {
  20. imageView.setImageResource(resId);
  21. }
  22. /**
  23. * 设置显示的文字
  24. */
  25. public void setTextViewText(String text) {
  26. textView.setText(text);
  27. }
  28. }
复制代码


在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。
源码打印?
  1. android:layout_width="fill_parent"
  2. android:layout_height="fill_parent"
  3. android:orientation="horizontal" >
  4. android:id="@+id/btn_right"
  5. android:layout_height="wrap_content"
  6. android:layout_width="wrap_content"
  7. android:background="@drawable/btn"
  8. />
  9. android:id="@+id/btn_error"
  10. android:layout_marginLeft="5dp"
  11. android:layout_height="wrap_content"
  12. android:layout_width="wrap_content"
  13. android:background="@drawable/btn"
  14. />
复制代码


这里用到了背景图片 在drawable/btn.xml
源码打印?
\
最后在activity中设置该控件,和其他控件差不多:
源码打印?
  1. public class IdentifyButtonActivity extends Activity {
  2. private ImageBtn imageBtn1;
  3. private ImageBtn imageBtn2;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. // TODO Auto-generated method stub
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.identifybutton);
  9. imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right);
  10. imageBtn2=(ImageBtn) this.findViewById(R.id.btn_error);
  11. imageBtn1.setTextViewText("确定");
  12. imageBtn2.setTextViewText("取消");
  13. imageBtn1.setImageResource(R.drawable.right_icon);
  14. imageBtn2.setImageResource(R.drawable.error_icon);
  15. imageBtn1.setOnClickListener(new View.OnClickListener() {
  16. public void onClick(View v) {
  17. // TODO Auto-generated method stub
  18. Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show();
  19. }
  20. });
  21. imageBtn2.setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View v) {
  23. // TODO Auto-generated method stub
  24. Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show();
  25. }
  26. });
  27. }
  28. }
复制代码


最后看看我们自定义控件的效果吧!
\
点击后还有按下按钮的效果。
android自定义控件实例2.jpg
本文地址:https://www.eechina.com/thread-119570-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关视频

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表