国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

【代碼】android自定義控件實例

發布時間:2013-8-15 16:25    發布者:reggae
關鍵詞: android
很多時候android常用的控件不能滿足我們的需求,那么我們就需要自定義一個控件了。今天做了一個自定義控件的實例,來分享下。
(PS:新建的QQ群,有興趣可以加入一起討論:Android學習交流群278744577,驗證:eec
首先定義一個layout實現按鈕內部布局:
源碼打印?

  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. }
復制代碼


最后看看我們自定義控件的效果吧!
\
點擊后還有按下按鈕的效果。

本文地址:http://m.qingdxww.cn/thread-119570-1-1.html     【打印本頁】

本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
您需要登錄后才可以發表評論 登錄 | 立即注冊

廠商推薦

  • Microchip視頻專區
  • Dev Tool Bits——使用MPLAB® Discover瀏覽資源
  • Dev Tool Bits——使用條件軟件斷點宏來節省時間和空間
  • Dev Tool Bits——使用DVRT協議查看項目中的數據
  • Dev Tool Bits——使用MPLAB® Data Visualizer進行功率監視
  • 貿澤電子(Mouser)專區

相關視頻

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日韩视频不卡 | 黄色免费网站观看 | 色综合久久中文色婷婷 | 手机看日韩毛片福利盒子 | 欧美成人www在线观看网页 | 国产高清大尺度一区二区不卡 | 成人欧美一区二区三区黑人妖 | 一区二区不卡久久精品 | 日本最新免费不卡二区在线 | 毛片在线视频在线播放 | 91伊人网 | 亚洲视频一区 | 逼逼无毛 | 日韩香蕉网 | 日本免费a视频 | 免费手机黄色网址 | 六月伊人 | 色综合欧美综合天天综合 | 国产午夜精品理论片在线 | 欧美一区二区三区免费 | 最近最新的日本免费 | 男女做羞羞高清视频免费 | 日韩在线免费视频 | 特赦1959全集免费观看 | 亚洲福利在线播放 | 被高跟鞋调教丨vk | 不卡影院在线观看 | 亚洲国产精品一区二区三区 | 亚洲色图第一页 | 日本国产欧美 | 中文字幕在线观看一区 | 日本一区高清视频 | 日日操夜夜操视频 | 亚洲欧美综合日韩字幕v在线 | 国产精品九九热 | 五月婷婷亚洲 | 成人影片在线 | 92精品国产自产在线 | 99久久国产综合精品网成人影院 | 天天摸夜夜添夜夜添国产 | 成人在线影视 |