android 仿Iphone底部 tab效果

如果你是android初学者,以前做过javaEE开发,或者说你有java基础。那么你学完了android 四大组件。就可以来看看这篇文章了。

四大组件我就不说了 网上一大把的文章 ,四大组件包括activity service Content Providers Intent

如果这篇文章你理解了,加上会xml解析 Json解析(json解析比xml更快,推荐大家以后使用json)就可以去面试android开发了

按钮放在底部是使用TabHost实现的。TabHost可以放在顶部 也可以放在底部。我之前已经发过三篇关于TabHost的文章

如果有不懂的 可以留言给我 ,也可以去看看我之前的三篇文章。 和这里相反的一篇文章是http://blog.csdn.net/sun6223508/article/details/6617545

转帖请注明出处http://blog.csdn.net/sun6223508


MainActivity.java

  1. publicclassMainActivityextendsTabActivity{
  2. privateRadioGroupgroup;
  3. privateTabHosttabHost;
  4. publicstaticfinalStringTAB_HOME="tabHome";
  5. publicstaticfinalStringTAB_MES="tabMes";
  6. publicstaticfinalStringTAB_TOUCH="tab_touch";
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. //TODOAuto-generatedmethodstub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.maintabs);
  12. group=(RadioGroup)findViewById(R.id.main_radio);
  13. tabHost=getTabHost();
  14. tabHost.addTab(tabHost.newTabSpec(TAB_HOME).setIndicator(TAB_HOME)
  15. .setContent(newIntent(this,Main.class)));
  16. tabHost.addTab(tabHost.newTabSpec(TAB_MES).setIndicator(TAB_MES)
  17. .setContent(newIntent(this,Main2.class)));
  18. tabHost.addTab(tabHost.newTabSpec(TAB_TOUCH).setIndicator(TAB_TOUCH)
  19. .setContent(newIntent(this,Main.class)));
  20. group.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  21. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  22. switch(checkedId){
  23. caseR.id.radio_button0:
  24. tabHost.setCurrentTabByTag(TAB_HOME);
  25. break;
  26. caseR.id.radio_button1:
  27. tabHost.setCurrentTabByTag(TAB_MES);
  28. break;
  29. caseR.id.radio_button2:
  30. tabHost.setCurrentTabByTag(TAB_TOUCH);
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. });
  37. }
  38. }

maintabs.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <TabHostandroid:id="@android:id/tabhost"android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
  4. <LinearLayoutandroid:orientation="vertical"
  5. android:layout_width="fill_parent"android:layout_height="fill_parent">
  6. <FrameLayoutandroid:id="@android:id/tabcontent"
  7. android:layout_width="fill_parent"android:layout_height="0.0dip"
  8. android:layout_weight="1.0"/>
  9. <TabWidgetandroid:id="@android:id/tabs"android:visibility="gone"
  10. android:layout_width="fill_parent"android:layout_height="wrap_content"
  11. android:layout_weight="0.0"/>
  12. <RadioGroupandroid:gravity="center_vertical"
  13. android:layout_gravity="bottom"android:orientation="horizontal"
  14. android:id="@+id/main_radio"android:background="@drawable/home_btn_bg"
  15. android:layout_width="fill_parent"android:layout_height="wrap_content">
  16. <RadioButtonandroid:id="@+id/radio_button0"android:tag="radio_button0"
  17. android:layout_marginTop="2.0dip"android:text="@string/main_home"android:background="@drawable/home_btn_bg"
  18. android:drawableTop="@drawable/icon_1_n"style="@style/main_tab_bottom"/>
  19. <RadioButtonandroid:id="@+id/radio_button1"android:tag="radio_button1"
  20. android:layout_marginTop="2.0dip"android:text="@string/main_message"android:background="@drawable/home_btn_bg"
  21. android:drawableTop="@drawable/icon_2_n"style="@style/main_tab_bottom"/>
  22. <RadioButtonandroid:id="@+id/radio_button2"android:tag="radio_button2"
  23. android:layout_marginTop="2.0dip"android:text="@string/main_comment"android:background="@drawable/home_btn_bg"
  24. android:drawableTop="@drawable/icon_3_n"style="@style/main_tab_bottom"/>
  25. </RadioGroup>
  26. </LinearLayout>
  27. </TabHost>

最关键就是上面这个XML了。它调用系统的tabhost


main.java

  1. publicclassMainextendsActivity{
  2. /**Calledwhentheactivityisfirstcreated.*/
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. }
  8. }

main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ImageViewandroid:text="@+id/Button01"android:id="@+id/Button01"
  6. android:layout_width="wrap_content"android:layout_height="wrap_content"
  7. android:background="@drawable/sfsd"></ImageView>
  8. </LinearLayout>

  1. publicclassMain2extendsActivity{
  2. @Override
  3. protectedvoidonCreate(BundlesavedInstanceState){
  4. //TODOAuto-generatedmethodstub
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main2);
  7. }
  8. }

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <ImageViewandroid:text="@+id/Button01"android:id="@+id/Button01"
  8. android:layout_width="wrap_content"android:layout_height="wrap_content"
  9. android:background="@drawable/dsfdsfds"></ImageView>
  10. </LinearLayout>

最关键的就是MainActivity.java这个文件 和它的layout
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值