免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2525 | 回复: 2
打印 上一主题 下一主题

自定义控件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-30 17:08 |只看该作者 |倒序浏览
自定义控件




定义一个/res/layout/control.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>     
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3. 3.    android:orientation="horizontal"     
  4. 4.    android:layout_width="fill_parent"     
  5. 5.    android:layout_height="fill_parent"     
  6. 6.    >     
  7. 7.<ImageView     
  8. 8.    android:layout_width="wrap_content"     
  9. 9.    android:layout_height="wrap_content"     
  10. 10.    android:id="@+id/iv"     
  11. 11.    android:src="@drawable/icon"     
  12. 12.    android:paddingTop="5dip"     
  13. 13.    android:paddingBottom="5dip"     
  14. 14.    android:paddingLeft="5dip"     
  15. 15.    android:layout_gravity="center_vertical"     
  16. 16.    />  
  17. 17.<TextView     
  18. 18.    android:layout_height="wrap_content"     
  19. 19.    android:text="确定"     
  20. 20.    android:textColor="#000000"     
  21. 21.    android:id="@+id/tv"     
  22. 22.    android:layout_marginLeft="8dip" android:layout_marginRight="10dip"   
  23. 23.    android:layout_gravity="center_vertical" android:layout_width="wrap_content"/>     
  24. 24.</LinearLayout>  
  25. <?xml version="1.0" encoding="utf-8"?>  
  26. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  27.     android:orientation="horizontal"  
  28.     android:layout_width="fill_parent"  
  29.     android:layout_height="fill_parent"  
  30.     >  
  31. <ImageView  
  32.     android:layout_width="wrap_content"  
  33.     android:layout_height="wrap_content"  
  34.     android:id="@+id/iv"  
  35.     android:src="@drawable/icon"  
  36.     android:paddingTop="5dip"  
  37.     android:paddingBottom="5dip"  
  38.     android:paddingLeft="5dip"  
  39.     android:layout_gravity="center_vertical"  
  40.     />
  41. <TextView  
  42.     android:layout_height="wrap_content"  
  43.     android:text="确定"  
  44.     android:textColor="#000000"  
  45.     android:id="@+id/tv"  
  46.     android:layout_marginLeft="8dip" android:layout_marginRight="10dip"
  47.     android:layout_gravity="center_vertical" android:layout_width="wrap_content"/>  
  48. </LinearLayout>
复制代码
自定义控件的类ImageBt.java

Java代码
  1. 1.package com.notice.ib;     
  2. 2.     
  3. 3.import com.control.R;   
  4. 4.  
  5. 5.import android.content.Context;     
  6. 6.import android.util.AttributeSet;     
  7. 7.import android.view.LayoutInflater;     
  8. 8.import android.widget.ImageView;     
  9. 9.import android.widget.LinearLayout;     
  10. 10.import android.widget.TextView;     
  11. 11.     
  12. 12.public class ImageBt extends LinearLayout {     
  13. 13.     
  14. 14.    private ImageView iv;     
  15. 15.    private TextView  tv;     
  16. 16.     
  17. 17.    public ImageBt(Context context) {     
  18. 18.        this(context, null);     
  19. 19.    }     
  20. 20.     
  21. 21.    public ImageBt(Context context, AttributeSet attrs) {     
  22. 22.        super(context, attrs);     
  23. 23.        // 导入布局     
  24. 24.        LayoutInflater.from(context).inflate(R.layout.control, this, true);     
  25. 25.        iv = (ImageView) findViewById(R.id.iv);     
  26. 26.        tv = (TextView) findViewById(R.id.tv);     
  27. 27.    }     
  28. 28.     
  29. 29.    /**   
  30. 30.     * 设置图片资源   
  31. 31.     */     
  32. 32.    public void setImageResource(int resId) {     
  33. 33.        iv.setImageResource(resId);     
  34. 34.    }     
  35. 35.     
  36. 36.    /**   
  37. 37.     * 设置显示的文字   
  38. 38.     */     
  39. 39.    public void setTextViewText(String text) {     
  40. 40.        tv.setText(text);     
  41. 41.    }     
  42. 42.     
  43. 43.}   
  44. package com.notice.ib;  
  45.   
  46. import com.control.R;

  47. import android.content.Context;  
  48. import android.util.AttributeSet;  
  49. import android.view.LayoutInflater;  
  50. import android.widget.ImageView;  
  51. import android.widget.LinearLayout;  
  52. import android.widget.TextView;  
  53.   
  54. public class ImageBt extends LinearLayout {  
  55.   
  56.     private ImageView iv;  
  57.     private TextView  tv;  
  58.   
  59.     public ImageBt(Context context) {  
  60.         this(context, null);  
  61.     }  
  62.   
  63.     public ImageBt(Context context, AttributeSet attrs) {  
  64.         super(context, attrs);  
  65.         // 导入布局  
  66.         LayoutInflater.from(context).inflate(R.layout.control, this, true);  
  67.         iv = (ImageView) findViewById(R.id.iv);  
  68.         tv = (TextView) findViewById(R.id.tv);  
  69.     }  
  70.   
  71.     /**
  72.      * 设置图片资源
  73.      */  
  74.     public void setImageResource(int resId) {  
  75.         iv.setImageResource(resId);  
  76.     }  
  77.   
  78.     /**
  79.      * 设置显示的文字
  80.      */  
  81.     public void setTextViewText(String text) {  
  82.         tv.setText(text);  
  83.     }  
  84.   
  85. }
复制代码
main.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="vertical"  
  4. 4.    android:layout_width="fill_parent"  
  5. 5.    android:layout_height="fill_parent"  
  6. 6.    >  
  7. 7.    <RelativeLayout     
  8. 8.         android:orientation="horizontal"     
  9. 9.         android:layout_width="fill_parent"     
  10. 10.         android:layout_height="wrap_content"     
  11. 11.         android:layout_gravity="center_vertical"     
  12. 12.         >     
  13. 13.         <com.notice.ib.ImageBt     
  14. 14.             android:id="@+id/bt_confirm"     
  15. 15.             android:layout_height="wrap_content"     
  16. 16.             android:layout_width="wrap_content"     
  17. 17.             android:layout_alignParentBottom="true"     
  18. 18.             android:background="#FFFFFF"  
  19. 19.             android:clickable="true"     
  20. 20.             android:focusable="true"     
  21. 21.             />     
  22. 22.         <com.notice.ib.ImageBt     
  23. 23.             android:id="@+id/bt_cancel"     
  24. 24.             android:layout_toRightOf="@id/bt_confirm"     
  25. 25.             android:layout_height="wrap_content"     
  26. 26.             android:layout_width="wrap_content"     
  27. 27.             android:layout_alignParentBottom="true"     
  28. 28.             android:background="#FFFFFF"     
  29. 29.             android:clickable="true"     
  30. 30.             android:focusable="true"     
  31. 31.             android:layout_marginLeft="10px"  
  32. 32.            />     
  33. 33.    </RelativeLayout>  
  34. 34.</LinearLayout>  
  35. <?xml version="1.0" encoding="utf-8"?>
  36. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  37.     android:orientation="vertical"
  38.     android:layout_width="fill_parent"
  39.     android:layout_height="fill_parent"
  40.     >
  41.         <RelativeLayout  
  42.          android:orientation="horizontal"  
  43.          android:layout_width="fill_parent"  
  44.          android:layout_height="wrap_content"  
  45.          android:layout_gravity="center_vertical"  
  46.          >  
  47.          <com.notice.ib.ImageBt  
  48.              android:id="@+id/bt_confirm"  
  49.              android:layout_height="wrap_content"  
  50.              android:layout_width="wrap_content"  
  51.              android:layout_alignParentBottom="true"  
  52.              android:background="#FFFFFF"
  53.              android:clickable="true"  
  54.              android:focusable="true"  
  55.              />  
  56.          <com.notice.ib.ImageBt  
  57.              android:id="@+id/bt_cancel"  
  58.              android:layout_toRightOf="@id/bt_confirm"  
  59.              android:layout_height="wrap_content"  
  60.              android:layout_width="wrap_content"  
  61.              android:layout_alignParentBottom="true"  
  62.              android:background="#FFFFFF"  
  63.              android:clickable="true"  
  64.              android:focusable="true"  
  65.              android:layout_marginLeft="10px"
  66.             />  
  67.         </RelativeLayout>
  68. </LinearLayout>
复制代码
主Activity类

Java代码
  1. 1.package com.control;   
  2. 2.  
  3. 3.import android.app.Activity;   
  4. 4.import android.os.Bundle;   
  5. 5.import android.view.View;   
  6. 6.import android.view.View.OnClickListener;   
  7. 7.  
  8. 8.import com.notice.ib.ImageBt;   
  9. 9.  
  10. 10.public class ControlActivity extends Activity {   
  11. 11.    private ImageBt ib1;     
  12. 12.    private ImageBt ib2;   
  13. 13.    /** Called when the activity is first created. */  
  14. 14.    @Override  
  15. 15.    public void onCreate(Bundle savedInstanceState) {   
  16. 16.        super.onCreate(savedInstanceState);   
  17. 17.        setContentView(R.layout.main);   
  18. 18.           
  19. 19.        ib1 = (ImageBt) findViewById(R.id.bt_confirm);     
  20. 20.        ib2 = (ImageBt) findViewById(R.id.bt_cancel);     
  21. 21.     
  22. 22.        ib1.setTextViewText("确定");     
  23. 23.        ib1.setImageResource(R.drawable.icon);     
  24. 24.        ib2.setTextViewText("取消");     
  25. 25.        ib2.setImageResource(R.drawable.icon);     
  26. 26.     
  27. 27.        ib1.setOnClickListener(new OnClickListener() {     
  28. 28.     
  29. 29.            @Override     
  30. 30.            public void onClick(View v) {     
  31. 31.                    //在这里可以实现点击事件     
  32. 32.            }     
  33. 33.        });   
  34. 34.    }   
  35. 35.}  
复制代码

论坛徽章:
0
2
发表于 2014-07-10 15:03
真心赞的控件知识分享

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
3 [报告]
发表于 2014-07-18 17:28 |只看该作者
支持,支持lz
lz 研究方向很专一的样子
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP