国产毛片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://www.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
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 蜜臀在线免费观看 | 国内精品成人女用 | 日韩中文字幕第一页 | 高清欧美一级在线观看 | 国产精品日韩欧美在线第3页 | 国产成人免费网站在线观看 | 日本福利片国产午夜久久 | 欧美日韩激情一区二区三区 | 老司机在线精品 | 国外在线观看网 | 午夜一级做a爰片久久毛片 午夜一级毛片看看 | 中文字幕成人免费视频 | 亚洲tv精品一区二区三区 | 一级毛片在线看 | 欧美一区二区三区精品国产 | 高清一区二区三区视频 | 国产资源精品一区二区免费 | 色视频免费观看高清完整 | 日本成人免费在线视频 | 99热首页| 天天操天天干天天舔 | 欧美日韩高清在线观看一区二区 | 麻豆视频污 | 草草影院第一页yycccom | 国产成人愉拍免费视频 | 日本久久久久久久中文字幕 | 99热精品在线播放 | 日韩免费在线播放 | 亚洲成人网在线 | 国产一卡二卡三卡四卡 | 亚洲三级视频在线 | 欧美日韩三级在线 | 午夜国产精品免费观看 | 在线观看樱花动漫 | 91精品国产免费青青碰在线观看 | 久久亚洲成人 | 亚洲国产精品综合久久网络 | 成年人网站免费在线观看 | 亚洲视频高清 | 五月天中文在线 | 日韩a在线观看 |