布局文件如下:

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns: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. <ViewFlipperandroid:id="@+id/flipper"android:layout_width="fill_parent"android:layout_height="fill_parent"/>
  8. <LinearLayoutandroid: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. <ImageButtonandroid: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. <ImageButtonandroid: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. <ImageButtonandroid: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>

代码如下:

        
  1. importandroid.app.Activity;
  2. importandroid.content.Intent;
  3. importandroid.os.Bundle;
  4. importandroid.view.LayoutInflater;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.ImageButton;
  8. importandroid.widget.ViewFlipper;
  9. publicclassSearchextendsActivity{
  10. privateImageButtonsearchBtn;
  11. privateImageButtonhistoryBtn;
  12. privateImageButtonstarredBtn;
  13. //通过ViewFlipper在多个view间转换
  14. privateViewFlipperflipper;
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. findViews();
  20. bindListener();
  21. }
  22. publicvoidfindViews(){
  23. searchBtn=(ImageButton)findViewById(R.id.searchBtn);
  24. historyBtn=(ImageButton)findViewById(R.id.historyBtn);
  25. starredBtn=(ImageButton)findViewById(R.id.starredBtn);
  26. flipper=(ViewFlipper)findViewById(R.id.flipper);
  27. LayoutInflaterlayoutInflater=this.getLayoutInflater();
  28. Viewsearch=layoutInflater.inflate(R.layout.search,null);
  29. Viewhistory=layoutInflater.inflate(R.layout.history,null);
  30. Viewstarred=layoutInflater.inflate(R.layout.starred,null);
  31. //将3个view加入ViewFlipper中
  32. flipper.addView(search,0);
  33. flipper.addView(history,1);
  34. flipper.addView(starred,2);
  35. //设置首先显示第一个view
  36. flipper.setDisplayedChild(0);
  37. }
  38. publicvoidbindListener(){
  39. searchBtn.setOnClickListener(newOnClickListener(){
  40. //点击第一个button,设置view为0
  41. @Override
  42. publicvoidonClick(Viewv){
  43. searchBtn.setImageResource(R.drawable.search_on);
  44. historyBtn.setImageResource(R.drawable.history);
  45. starredBtn.setImageResource(R.drawable.starred);
  46. flipper.setDisplayedChild(0);
  47. }
  48. });
  49. historyBtn.setOnClickListener(newOnClickListener(){
  50.  //点击第二个button,设置view为1
  51. @Override
  52. publicvoidonClick(Viewv){
  53. searchBtn.setImageResource(R.drawable.search);
  54. historyBtn.setImageResource(R.drawable.history_on);
  55. starredBtn.setImageResource(R.drawable.starred);
  56. flipper.setDisplayedChild(1);
  57. }
  58. });
  59. starredBtn.setOnClickListener(newOnClickListener(){
  60.  //点击第三个button,设置view为2
  61. @Override
  62. publicvoidonClick(Viewv){
  63. searchBtn.setImageResource(R.drawable.search);
  64. historyBtn.setImageResource(R.drawable.history);
  65. starredBtn.setImageResource(R.drawable.starred_on);
  66. flipper.setDisplayedChild(2);
  67. }
  68. });
  69. }
  70. }

程序截图:

更多相关文章

  1. android之Tab选项卡控件
  2. android radiobutton选中字体颜色改变的方法
  3. 【Android】Vibrator(震动模式)设置长短震动
  4. Android窗口机制(一)——Window,PhoneWindow,DecorView理解
  5. Android中两个控件滑动效果冲突的解决办法
  6. android 上传文件到服务器
  7. Android窗口浮在屏幕上效果
  8. Android-线性布局的经典案例1-计算器
  9. Android与Javascript交互示例(一)

随机推荐

  1. 【SQL刷题系列】:leetcode177 Nth Highest
  2. 【机器学习笔记】:从零开始学会逻辑回归(一
  3. 5、Shell命令脚本
  4. 你知道Jupyter notebook还可以用来做 “
  5. 【机器学习笔记】:大话线性回归(一)
  6. 【机器学习笔记】:逻辑回归实战练习(二)
  7. 分布式爬虫的部署之Scrapyd分布式部署
  8. Python操作Redis,你要的都在这了!
  9. centos 网络配置1
  10. 如果我是Android面试官九