<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    【代碼】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.portaltwn.com/thread-119570-1-1.html     【打印本頁】

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

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷