Android imitates buttonbar

Source: Internet
Author: User

The layout file is as follows:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
  8.     <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" 
  9.      android:background="@drawable/bottom_bg" android:layout_alignParentBottom="true" android:gravity="center_vertical" style="@android:style/ButtonBar"> 
  10.      <ImageButton android:id="@+id/searchBtn" android:src="@drawable/search_on" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> 
  11.      <ImageButton android:id="@+id/historyBtn" android:src="@drawable/history" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> 
  12.      <ImageButton android:id="@+id/starredBtn" android:src="@drawable/starred" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> 
  13.     </LinearLayout> 
  14. </RelativeLayout> 

The Code is as follows:

 

 
 
  1. Import Android. App. activity;
  2. Import Android. content. intent;
  3. Import Android. OS. Bundle;
  4. Import Android. View. layoutinflater;
  5. Import Android. View. view;
  6. Import Android. View. View. onclicklistener;
  7. Import Android. widget. imagebutton;
  8. Import Android. widget. viewflipper;
  9. Public Class search extends activity {
  10. Private imagebutton searchbtn;
  11. Private imagebutton historybtn;
  12. Private imagebutton starredbtn;
  13. // Convert multiple views through viewflipper
  14. Private viewflipper Flipper;
  15. @ Override
  16. Public void oncreate (bundle savedinstancestate ){
  17. Super. oncreate (savedinstancestate );
  18. Setcontentview (R. layout. Main );
  19. Findviews ();
  20. Bindlistener ();
  21. }
  22. Public void findviews (){
  23. Searchbtn = (imagebutton) findviewbyid (R. Id. searchbtn );
  24. Historybtn = (imagebutton) findviewbyid (R. Id. historybtn );
  25. Starredbtn = (imagebutton) findviewbyid (R. Id. starredbtn );
  26.  
  27. Flipper = (viewflipper) findviewbyid (R. Id. Flipper );
  28.  
  29. Layoutinflater = This. getlayoutinflater ();
  30. View search = layoutinflater. Inflate (R. layout. Search, null );
  31. View history = layoutinflater. Inflate (R. layout. History, null );
  32. View starred = layoutinflater. Inflate (R. layout. starred, null );
  33. // Add three views to viewflipper
  34. Flipper. addview (search, 0 );
  35. Flipper. addview (history, 1 );
  36. Flipper. addview (starred, 2 );
  37. // Set to display the first view
  38. Flipper. setdisplayedchild (0 );
  39. }
  40. Public void bindlistener (){
  41. Searchbtn. setonclicklistener (New onclicklistener (){
  42. // Click the first button and set the view to 0.
  43. @ Override
  44. Public void onclick (view v ){
  45. Searchbtn. setimageresource (R. drawable. search_on );
  46. Historybtn. setimageresource (R. drawable. History );
  47. Starredbtn. setimageresource (R. drawable. starred );
  48. Flipper. setdisplayedchild (0 );
  49. }
  50. });
  51. Historybtn. setonclicklistener (New onclicklistener (){
  52. // Click the second button and set view to 1
  53. @ Override
  54. Public void onclick (view v ){
  55. Searchbtn. setimageresource (R. drawable. Search );
  56. Historybtn. setimageresource (R. drawable. history_on );
  57. Starredbtn. setimageresource (R. drawable. starred );
  58. Flipper. setdisplayedchild (1 );
  59. }
  60. });
  61. Starredbtn. setonclicklistener (New onclicklistener (){
  62. // Click the third button and set view to 2
  63. @ Override
  64. Public void onclick (view v ){
  65. Searchbtn. setimageresource (R. drawable. Search );
  66. Historybtn. setimageresource (R. drawable. History );
  67. Starredbtn. setimageresource (R. drawable. starred_on );
  68. Flipper. setdisplayedchild (2 );
  69. }
  70. });
  71. }
  72. }

Program:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.