閱讀940 返回首頁    go 京東網上商城


自定義TabHost實現背景圖片隨選項卡切換滑動效果

https://download.csdn.net/detail/hfsu0419/4137990



本例子是對TabHost組件的自定義,實現標簽居底顯示;每個標簽包含圖片和文字。

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="https://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent" >
  6. <RelativeLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:background="#F8FFEE" >
  10. <!-- 內容顯示 -->
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content" >
  15. <TextView
  16. android:id="@+id/text1"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:text="@string/text1"
  20. android:textSize="32dp" />
  21. <TextView
  22. android:id="@+id/text2"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:text="@string/text2"
  26. android:textSize="32dp" />
  27. <TextView
  28. android:id="@+id/text3"
  29. android:layout_width="fill_parent"
  30. android:layout_height="fill_parent"
  31. android:text="@string/text3"
  32. android:textSize="32dp" />
  33. </FrameLayout>
  34. <TabWidget
  35. android:id="@android:id/tabs"
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_alignParentBottom="true"
  39. android:background="@drawable/bottom_tab_bg" >
  40. </TabWidget>
  41. <!-- 選項卡背景圖片 -->
  42. <ImageView
  43. android:id="@+id/tab_top_select"
  44. android:layout_width="45dp"
  45. android:layout_height="45dp"
  46. android:layout_alignParentBottom="true"
  47. android:src="@drawable/topbar_select" />
  48. </RelativeLayout>
  49. </TabHost>
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:andro android: android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#F8FFEE" > <!-- 內容顯示 --> <FrameLayout android: android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text1" android:textSize="32dp" /> <TextView android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text2" android:textSize="32dp" /> <TextView android: android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/text3" android:textSize="32dp" /> </FrameLayout> <TabWidget android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/bottom_tab_bg" > </TabWidget> <!-- 選項卡背景圖片 --> <ImageView android: android:layout_width="45dp" android:layout_height="45dp" android:layout_alignParentBottom="true" android:src="@drawable/topbar_select" /> </RelativeLayout> </TabHost>
MainActivity.java

  1. package com.suxh;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.TabActivity;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.util.Log;
  10. import android.view.Gravity;
  11. import android.view.View;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import android.widget.TabHost;
  16. import android.widget.TabHost.OnTabChangeListener;
  17. import android.widget.TextView;
  18. public class MainActivity extends TabActivity implements OnTabChangeListener {
  19. // 當前選中的Tab標號
  20. private int mCurSelectTabIndex = 0;
  21. // 默認選中第一個tab頁 移動標誌操作
  22. private final int INIT_SELECT = 0;
  23. // 滑動動畫執行時間
  24. private final int DELAY_TIME = 500;
  25. private TabHost mTabHost;
  26. // 存放Tab頁中ImageView信息
  27. public List<ImageView> imageList = new ArrayList<ImageView>();
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. // 取得TabHost對象
  33. mTabHost = getTabHost();
  34. /* 為TabHost添加標簽 */
  35. mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(composeLayout("優惠信息", R.drawable.pic1_s)).setContent(R.id.text1));
  36. mTabHost.addTab(mTabHost.newTabSpec("tab_2").setIndicator(composeLayout("銀行公告", R.drawable.pic2_n)).setContent(R.id.text2));
  37. mTabHost.addTab(mTabHost.newTabSpec("tab_3").setIndicator(composeLayout("金融產品", R.drawable.pic3_n)).setContent(R.id.text3));
  38. // 設置TabHost的背景顏色
  39. mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
  40. // 設置當前選中的Tab頁
  41. mTabHost.setCurrentTab(0);
  42. // TabHost添加事件
  43. mTabHost.setOnTabChangedListener(this);
  44. // 初始化移動標識這裏移到第一個tab頁
  45. initCurSelectTab();
  46. }
  47. /**
  48. * 初始化選中Tab覆蓋圖片的Handler
  49. */
  50. private Handler initSelectTabHandle = new Handler () {
  51. public void handleMessage (Message msg) {
  52. switch (msg.what) {
  53. case INIT_SELECT:
  54. moveTopSelect(INIT_SELECT);
  55. break;
  56. default:
  57. break;
  58. }
  59. super.handleMessage(msg);
  60. }
  61. };
  62. /**
  63. * 初始化選中Tab覆蓋圖片
  64. */
  65. public void initCurSelectTab(){
  66. // 默認選中移動圖片位置
  67. Message msg = new Message();
  68. msg.what = INIT_SELECT;
  69. initSelectTabHandle.sendMessageDelayed(msg, DELAY_TIME);
  70. }
  71. /**
  72. * Tab頁改變
  73. */
  74. public void onTabChanged(String tabId) {
  75. // 設置所有選項卡的圖片為未選中圖片
  76. imageList.get(0).setImageDrawable(getResources().getDrawable(R.drawable.pic1_n));
  77. imageList.get(1).setImageDrawable(getResources().getDrawable(R.drawable.pic2_n));
  78. imageList.get(2).setImageDrawable(getResources().getDrawable(R.drawable.pic3_n));
  79. if (tabId.equalsIgnoreCase("tab_1")) {
  80. imageList.get(0).setImageDrawable(getResources().getDrawable(R.drawable.pic1_s));
  81. // 移動底部背景圖片
  82. moveTopSelect(0);
  83. } else if (tabId.equalsIgnoreCase("tab_2")) {
  84. imageList.get(1).setImageDrawable(getResources().getDrawable(R.drawable.pic2_s));
  85. // 移動底部背景圖片
  86. moveTopSelect(1);
  87. } else if (tabId.equalsIgnoreCase("tab_3")) {
  88. imageList.get(2).setImageDrawable(getResources().getDrawable(R.drawable.pic3_s));
  89. // 移動底部背景圖片
  90. moveTopSelect(2);
  91. }
  92. }
  93. /**
  94. * 移動tab選中標識圖片
  95. * @param selectIndex
  96. * @param curIndex
  97. */
  98. public void moveTopSelect(int selectIndex) {
  99. View topSelect = (View) findViewById(R.id.tab_top_select);
  100. // 起始位置中心點
  101. int startMid = ((View) getTabWidget().getChildAt(mCurSelectTabIndex)).getLeft() + ((View) getTabWidget().getChildAt(mCurSelectTabIndex)).getWidth() / 2;
  102. // 起始位置左邊位置坐標
  103. int startLeft = startMid - topSelect.getWidth() / 2;
  104. // 目標位置中心點
  105. int endMid = ((View) getTabWidget().getChildAt(selectIndex)).getLeft() + ((View) getTabWidget().getChildAt(selectIndex)).getWidth() / 2;
  106. // 目標位置左邊位置坐標
  107. int endLeft = endMid - topSelect.getWidth() / 2;
  108. TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - topSelect.getLeft(), 0, 0);
  109. animation.setDuration(200);
  110. animation.setFillAfter(true);
  111. topSelect.bringToFront();
  112. topSelect.startAnimation(animation);
  113. // 改變當前選中按鈕索引
  114. mCurSelectTabIndex = selectIndex;
  115. Log.i("fs", "endMid " + endMid + " startLeft " + startLeft + " endLeft" + (endLeft - topSelect.getLeft()));
  116. }
  117. /**
  118. * 這個設置Tab標簽本身的布局,需要TextView和ImageView不能重合 s:是文本顯示的內容 i:是ImageView的圖片位置
  119. */
  120. public View composeLayout(String s, int i) {
  121. // 定義一個LinearLayout布局
  122. LinearLayout layout = new LinearLayout(this);
  123. // 設置布局垂直顯示
  124. layout.setOrientation(LinearLayout.VERTICAL);
  125. ImageView iv = new ImageView(this);
  126. imageList.add(iv);
  127. iv.setImageResource(i);
  128. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  129. lp.setMargins(0, 5, 0, 0);
  130. layout.addView(iv, lp);
  131. // 定義TextView
  132. TextView tv = new TextView(this);
  133. tv.setGravity(Gravity.CENTER);
  134. tv.setSingleLine(true);
  135. tv.setText(s);
  136. tv.setTextColor(Color.WHITE);
  137. tv.setTextSize(10);
  138. layout.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  139. return layout;
  140. }
  141. }


最後更新:2017-04-02 17:51:26

  上一篇:go GridView + ViewFlipper布局界麵,模仿“機鋒市場”
  下一篇:go jQuery的ajax傳遞時亂碼解決