今天终于把公司的界面原型做完了,哈哈,提前完成正好趁现在有时间更新下最近学到的一些特殊效果。其中这个仿微信的底部菜单,真的要感谢家辉兄弟,我才得以解决。首先看一下实现后效果。

就下面的那个底部栏,下面看一下实现代码吧!

首先是布局main.xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <TabHostandroid: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. <LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
  5. <FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="0.0dip"android:layout_weight="1.0"/>
  6. <TabWidgetandroid:id="@android:id/tabs"android:visibility="gone"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="0.0"/>
  7. <LinearLayoutandroid:gravity="bottom"android:layout_gravity="bottom"android:orientation="horizontal"android:id="@+id/main_tab_group"android:background="@drawable/bottom1"android:paddingTop="2.0dip"android:layout_width="fill_parent"android:layout_height="wrap_content">
  8. <FrameLayoutandroid:background="@null"android:layout_width="0.0dip"android:layout_height="fill_parent"android:layout_weight="1.0">
  9. <LinearLayoutandroid:id="@+id/main_layout_addExam"android:gravity="bottom|center"android:layout_width="fill_parent"android:layout_height="fill_parent">
  10. <RadioButtonandroid:id="@+id/main_tab_addExam"android:checked="true"android:text="添加考试"android:drawableTop="@drawable/label_1"style="@style/MMTabButton"/>
  11. </LinearLayout>
  12. <LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout_width="fill_parent"android:layout_height="fill_parent">
  13. <TextViewandroid:textSize="10.0dip"android:text="1"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main_tab_unread_tv"android:background="@drawable/new_icon_2"android:visibility="invisible"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  14. </LinearLayout>
  15. </FrameLayout>
  16. <FrameLayoutandroid:background="@null"android:layout_width="0.0dip"android:layout_height="fill_parent"android:layout_weight="1.0">
  17. <LinearLayoutandroid:id="@+id/main_layout_myExam"android:gravity="bottom|center"android:layout_width="fill_parent"android:layout_height="fill_parent">
  18. <RadioButtonandroid:id="@+id/main_tab_myExam"android:text="我的考试"android:drawableTop="@drawable/label_2"style="@style/MMTabButton"/>
  19. </LinearLayout>
  20. <LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout_width="fill_parent"android:layout_height="fill_parent">
  21. <TextViewandroid:textSize="10.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main_tab_address"android:background="@drawable/new_icon_2"android:visibility="invisible"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"/>
  22. </LinearLayout>
  23. </FrameLayout>
  24. <FrameLayoutandroid:background="@null"android:layout_width="0.0dip"android:layout_height="fill_parent"android:layout_weight="1.0">
  25. <LinearLayoutandroid:id="@+id/main_layout_message"android:gravity="bottom|center"android:layout_width="fill_parent"android:layout_height="fill_parent">
  26. <RadioButtonandroid:id="@+id/main_tab_message"android:text="考试资讯"android:drawableTop="@drawable/label_3"style="@style/MMTabButton"/>
  27. </LinearLayout>
  28. <LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout_width="fill_parent"android:layout_height="fill_parent">
  29. <TextViewandroid:textSize="12.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main_tab_new_tv"android:background="@drawable/new_icon_2"android:visibility="visible"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"/>
  30. </LinearLayout>
  31. </FrameLayout>
  32. <FrameLayoutandroid:background="@null"android:layout_width="0.0dip"android:layout_height="fill_parent"android:layout_weight="1.0">
  33. <LinearLayoutandroid:id="@+id/main_layout_settings"android:gravity="bottom|center"android:layout_width="fill_parent"android:layout_height="fill_parent">
  34. <RadioButtonandroid:id="@+id/main_tab_settings"android:text="设置"android:drawableTop="@drawable/label4"style="@style/MMTabButton"/>
  35. </LinearLayout>
  36. <LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout_width="fill_parent"android:layout_height="fill_parent">
  37. <TextViewandroid:textSize="10.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main_tab_setting_new_tv"android:background="@drawable/new_icon_2"android:paddingLeft="6.0dip"android:paddingRight="6.0dip"android:visibility="invisible"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"/>
  38. </LinearLayout>
  39. </FrameLayout>
  40. </LinearLayout>
  41. </LinearLayout>
  42. </TabHost>

其实就是用FrameLayout,微信也是这样布局的我反编译过。这样就可以灵活的布局那个红色的图标了。

下面看一下activity切换代码:

[html] view plain copy
  1. importandroid.app.TabActivity;
  2. importandroid.content.Intent;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.view.View.OnClickListener;
  6. importandroid.view.Window;
  7. importandroid.widget.LinearLayout;
  8. importandroid.widget.RadioButton;
  9. importandroid.widget.TabHost;
  10. importandroid.widget.TextView;
  11. publicclassMainTabActivityextendsTabActivity{
  12. TabHosttabHost;
  13. privateTextViewmain_tab_unread_tv;
  14. privateRadioButtonmain_tab_addExam,main_tab_myExam,main_tab_message,main_tab_settings;
  15. privateLinearLayoutmain_layout_addExam,main_layout_myExam,main_layout_message,main_layout_settings;
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  19. setContentView(R.layout.main);
  20. initTab();
  21. init();
  22. }
  23. privatevoidinit(){
  24. main_tab_addExam=(RadioButton)findViewById(R.id.main_tab_addExam);
  25. main_tab_myExam=(RadioButton)findViewById(R.id.main_tab_myExam);
  26. main_tab_message=(RadioButton)findViewById(R.id.main_tab_message);
  27. main_tab_settings=(RadioButton)findViewById(R.id.main_tab_settings);
  28. main_layout_addExam=(LinearLayout)findViewById(R.id.main_layout_addExam);
  29. main_layout_myExam=(LinearLayout)findViewById(R.id.main_layout_myExam);
  30. main_layout_message=(LinearLayout)findViewById(R.id.main_layout_message);
  31. main_layout_settings=(LinearLayout)findViewById(R.id.main_layout_settings);
  32. main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_one_one),null,null);
  33. main_layout_addExam.setBackgroundResource(R.drawable.labelbg);
  34. main_tab_unread_tv=(TextView)findViewById(R.id.main_tab_unread_tv);
  35. //main_tab_unread_tv.setVisibility(View.VISIBLE);
  36. //main_tab_unread_tv.setText("3");
  37. main_tab_addExam.setOnClickListener(newOnClickListener(){
  38. publicvoidonClick(Viewarg0){
  39. tabHost.setCurrentTabByTag("first");
  40. /*main_tab_addExam.setBackgroundResource(R.drawable.label_one_one);
  41. main_tab_myExam.setBackgroundResource(R.drawable.label_2);
  42. main_tab_message.setBackgroundResource(R.drawable.label_3);*/
  43. //main_tab_addExam.setCompoundDrawables(null,getResources().getDrawable(R.drawable.label_one_one),null,null);
  44. main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_one_one),null,null);
  45. main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_2),null,null);
  46. main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_3),null,null);
  47. main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label4),null,null);
  48. main_layout_addExam.setBackgroundResource(R.drawable.labelbg);
  49. main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  50. main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  51. main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  52. }
  53. });
  54. main_tab_myExam.setOnClickListener(newOnClickListener(){
  55. publicvoidonClick(Viewarg0){
  56. tabHost.setCurrentTabByTag("second");
  57. /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);
  58. main_tab_myExam.setBackgroundResource(R.drawable.label_two_one);
  59. main_tab_message.setBackgroundResource(R.drawable.label_3);*/
  60. main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_1),null,null);
  61. main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_two_one),null,null);
  62. main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_3),null,null);
  63. main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label4),null,null);
  64. main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  65. main_layout_myExam.setBackgroundResource(R.drawable.labelbg);
  66. main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  67. main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  68. }
  69. });
  70. main_tab_message.setOnClickListener(newOnClickListener(){
  71. publicvoidonClick(Viewv){
  72. //TODOAuto-generatedmethodstub
  73. tabHost.setCurrentTabByTag("third");
  74. /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);
  75. main_tab_myExam.setBackgroundResource(R.drawable.label_2);
  76. main_tab_message.setBackgroundResource(R.drawable.label_three_one);*/
  77. main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_1),null,null);
  78. main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_2),null,null);
  79. main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_three_one),null,null);
  80. main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label4),null,null);
  81. main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  82. main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  83. main_layout_message.setBackgroundResource(R.drawable.labelbg);
  84. main_layout_settings.setBackgroundResource(R.drawable.mm_trans);
  85. }
  86. });
  87. main_tab_settings.setOnClickListener(newOnClickListener(){
  88. publicvoidonClick(Viewv){
  89. //TODOAuto-generatedmethodstub
  90. tabHost.setCurrentTabByTag("four");
  91. main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_1),null,null);
  92. main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_2),null,null);
  93. main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_3),null,null);
  94. main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.label_four_one),null,null);
  95. main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);
  96. main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);
  97. main_layout_message.setBackgroundResource(R.drawable.mm_trans);
  98. main_layout_settings.setBackgroundResource(R.drawable.labelbg);
  99. }
  100. });
  101. }
  102. privatevoidinitTab(){
  103. tabHost=getTabHost();
  104. tabHost.addTab(tabHost.newTabSpec("first").setIndicator("first").setContent(
  105. newIntent(this,AddExamActivity.class)));
  106. tabHost.addTab(tabHost.newTabSpec("second").setIndicator("second").setContent(
  107. newIntent(this,MyExamActivity.class)));
  108. tabHost.addTab(tabHost.newTabSpec("third").setIndicator("third").setContent(
  109. newIntent(this,MessageActivity.class)));
  110. tabHost.addTab(tabHost.newTabSpec("four").setIndicator("four").setContent(
  111. newIntent(this,SettingActivity.class)));
  112. }
  113. }
setCompoundDrawablesWithIntrinsicBounds方法是我用来设置顶部图片的,用来替换android:drawableTop这个属性的背景图,至于为什么那么麻烦每次都要设置背景图片颜色,是因为没用用RadioGroup包含RadioButton,所以RadioButton就不能做到单个切换。

更多相关文章

  1. 布局添加下划线
  2. Android中DialogFragment的简单使用及常见问题
  3. getLeft,getTop,getRight,getBottm区别
  4. Android(安卓)界面适配笔记
  5. Android(安卓)Adapter总结
  6. Android(安卓)开发学习进程0.15 adb cardview framelayout 控件
  7. android notification的用法
  8. Android复习资料1
  9. Android(安卓)ActionBar使用

随机推荐

  1. Android 源码目录结构
  2. Android中XML解析-Dom解析
  3. ANDROID笔记:TextView的简单使用
  4. 【翻译】安卓新播放器EXOplayer介绍
  5. android 短信接收
  6. android自动化测试工具---monkeyrunner工
  7. Android NDK学习 Android.mk实例和NDK实
  8. 飞速浏览器3.0.0 webview 练手项目.
  9. android学习笔记17:文字设置
  10. 跑马灯效果实现方式三种