新浪微博布局学习——妙用TabHost

来源:互联网 发布:linux自动重启脚本 编辑:程序博客网 时间:2024/04/29 03:25

 http://www.apkbus.com/forum.php?mod=viewthread&tid=3436

 

正文

    maintabs.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  5. <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
  6. <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />
  7. <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">
  8. <RadioButton android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
  9. <RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
  10. <RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
  11. <RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
  12. <RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />
  13. </RadioGroup>
  14. </LinearLayout>
  15. </TabHost>
复制代码

    styles.xml
  1. <style name="main_tab_bottom">
  2. <item name="android:textSize">@dimen/bottom_tab_font_size</item>
  3. <item name="android:textColor">#ffffffff</item>
  4. <item name="android:ellipsize">marquee</item>
  5. <item name="android:gravity">center_horizontal</item>
  6. <item name="android:background">@drawable/home_btn_bg</item>
  7. <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
  8. <item name="android:layout_width">fill_parent</item>
  9. <item name="android:layout_height">wrap_content</item>
  10. <item name="android:button">@null</item>
  11. <item name="android:singleLine">true</item>
  12. <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
  13. <item name="android:layout_weight">1.0</item>
  14. </style>
复制代码

home_btn_bg.xml
  1. <selector
  2. xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
  4. <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
  5. <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
  6. <item android:drawable="@drawable/transparent" />
  7. </selector>
复制代码

 代码说明:        1.  需要注意的是他这里把TabWidget的Visibility设置成了gone!也就是默认难看的风格不见了,取而代之的是5个带风格的单选按钮.
        2.  注意为单选按钮设置的style,其中最重要的是为其background设置了home_btn_bg.xml,也就是自定义了选中效果。
    Java文件
  1. public class MainTabActivity extends TabActivity implements
  2. OnCheckedChangeListener {

  3. private TabHost mHost;
  4. private Intent mMBlogIntent;
  5. private Intent mMoreIntent;
  6. private Intent mInfoIntent;
  7. private Intent mSearchIntent;
  8. private Intent mUserInfoIntent;

  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. requestWindowFeature(Window.FEATURE_NO_TITLE);
  13. setContentView(R.layout.maintabs);

  14. // ~~~~~~~~~~~~ 初始化
  15. this.mMBlogIntent = new Intent(this, HomeListActivity.class);
  16. this.mSearchIntent = new Intent(this, SearchSquareActivity.class);
  17. this.mInfoIntent = new Intent(this, MessageGroup.class);
  18. this.mUserInfoIntent = new Intent(this, MyInfoActivity.class);
  19. this.mMoreIntent = new Intent(this, MoreItemsActivity.class);

  20. initRadios();

  21. setupIntent();
  22. }

  23. /**
  24. * 初始化底部按钮
  25. */
  26. private void initRadios() {
  27. ((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this);
  28. ((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this);
  29. ((RadioButton) findViewById(R.id.radio_button2)).setOnCheckedChangeListener(this);
  30. ((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(this);
  31. ((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(this);
  32. }

  33. /**
  34. * 切换模块
  35. */
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  38. if (isChecked) {
  39. switch (buttonView.getId()) {
  40. case R.id.radio_button0:
  41. this.mHost.setCurrentTabByTag("mblog_tab");
  42. break;
  43. case R.id.radio_button1:
  44. this.mHost.setCurrentTabByTag("message_tab");
  45. break;
  46. case R.id.radio_button2:
  47. this.mHost.setCurrentTabByTag("userinfo_tab");
  48. break;
  49. case R.id.radio_button3:
  50. this.mHost.setCurrentTabByTag("search_tab");
  51. break;
  52. case R.id.radio_button4:
  53. this.mHost.setCurrentTabByTag("more_tab");
  54. break;
  55. }
  56. }
  57. }

  58. private void setupIntent() {
  59. this.mHost = getTabHost();
  60. TabHost localTabHost = this.mHost;

  61. localTabHost.addTab(buildTabSpec("mblog_tab", R.string.main_home,
  62. R.drawable.icon_1_n, this.mMBlogIntent));

  63. localTabHost.addTab(buildTabSpec("message_tab", R.string.main_news,
  64. R.drawable.icon_2_n, this.mInfoIntent));

  65. localTabHost.addTab(buildTabSpec("userinfo_tab", R.string.main_my_info,
  66. R.drawable.icon_3_n, this.mUserInfoIntent));

  67. localTabHost.addTab(buildTabSpec("search_tab", R.string.menu_search,
  68. R.drawable.icon_4_n, this.mSearchIntent));

  69. localTabHost.addTab(buildTabSpec("more_tab", R.string.more,
  70. R.drawable.icon_5_n, this.mMoreIntent));

  71. }

  72. private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
  73. final Intent content) {
  74. return this.mHost
  75. .newTabSpec(tag)
  76. .setIndicator(getString(resLabel),
  77. getResources().getDrawable(resIcon))
  78. .setContent(content);
  79. }
复制代码

代码说明      1.  由于TabWidget被隐藏,所以相关的事件也会无效,这里取巧用RadioGroup与RadioButton的特性来处理切换,然后监听事件调用setCurrentTabByTag来切换Activity。
      2.  注意即使TabWidget被隐藏,也要为其设置indicator,否则会保持。

  三、总结

    在这之前如果要做这种效果我恐怕第一时间就会想到用ActivityGroup来做,主要是因为TabHost的TabWidget非常难看,用起来也不方便。其实从源码可以看出,TabActivity也是继承自ActivityGroup,这里结合了单选按钮和TabHost,各取其长,有时间可以专门写一个这样的自定义控件
原创粉丝点击