android 仿Iphone底部 tab效果

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

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

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

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

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

转帖请注明出处 http://blog.youkuaiyun.com/sun6223508  


MainActivity.java

  1. public class MainActivity extends TabActivity {  
  2.     private RadioGroup group;  
  3.     private TabHost tabHost;  
  4.     public static final String TAB_HOME = "tabHome";  
  5.     public static final String TAB_MES = "tabMes";  
  6.     public static final String TAB_TOUCH = "tab_touch";  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         // TODO Auto-generated method stub  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.maintabs);  
  13.         group = (RadioGroup) findViewById(R.id.main_radio);  
  14.         tabHost = getTabHost();  
  15.         tabHost.addTab(tabHost.newTabSpec(TAB_HOME).setIndicator(TAB_HOME)  
  16.                 .setContent(new Intent(this, Main.class)));  
  17.         tabHost.addTab(tabHost.newTabSpec(TAB_MES).setIndicator(TAB_MES)  
  18.                 .setContent(new Intent(this, Main2.class)));  
  19.         tabHost.addTab(tabHost.newTabSpec(TAB_TOUCH).setIndicator(TAB_TOUCH)  
  20.                 .setContent(new Intent(this, Main.class)));  
  21.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  22.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  23.                 switch (checkedId) {  
  24.                 case R.id.radio_button0:  
  25.                     tabHost.setCurrentTabByTag(TAB_HOME);  
  26.                     break;  
  27.                 case R.id.radio_button1:  
  28.                     tabHost.setCurrentTabByTag(TAB_MES);  
  29.                     break;  
  30.                 case R.id.radio_button2:  
  31.                     tabHost.setCurrentTabByTag(TAB_TOUCH);  
  32.                     break;  
  33.   
  34.                 default:  
  35.                     break;  
  36.                 }  
  37.             }  
  38.         });  
  39.     }  
  40. }  

maintabs.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <TabHost android: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.     <LinearLayout android:orientation="vertical"  
  5.         android:layout_width="fill_parent" android:layout_height="fill_parent">  
  6.   
  7.         <FrameLayout android:id="@android:id/tabcontent"  
  8.             android:layout_width="fill_parent" android:layout_height="0.0dip"  
  9.             android:layout_weight="1.0" />  
  10.         <TabWidget android:id="@android:id/tabs" android:visibility="gone"  
  11.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  12.             android:layout_weight="0.0" />  
  13.         <RadioGroup android:gravity="center_vertical"  
  14.             android:layout_gravity="bottom" android:orientation="horizontal"  
  15.             android:id="@+id/main_radio" android:background="@drawable/home_btn_bg"  
  16.             android:layout_width="fill_parent" android:layout_height="wrap_content">  
  17.             <RadioButton android:id="@+id/radio_button0" android:tag="radio_button0"  
  18.                 android:layout_marginTop="2.0dip" android:text="@string/main_home" android:background="@drawable/home_btn_bg"  
  19.                 android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />  
  20.             <RadioButton android:id="@+id/radio_button1" android:tag="radio_button1"  
  21.                 android:layout_marginTop="2.0dip" android:text="@string/main_message" android:background="@drawable/home_btn_bg"  
  22.                 android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />  
  23.             <RadioButton android:id="@+id/radio_button2" android:tag="radio_button2"  
  24.                 android:layout_marginTop="2.0dip" android:text="@string/main_comment"  android:background="@drawable/home_btn_bg"  
  25.                 android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />  
  26.   
  27.         </RadioGroup>  
  28.     </LinearLayout>  
  29. </TabHost>  

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


 main.java

  1. public class Main extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.          
  8.     }  
  9.   
  10.       
  11. }  

main.xml

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

  1. public class Main2 extends Activity{  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         // TODO Auto-generated method stub  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main2);  
  7.           
  8.     }  
  9. }  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.     <ImageView android: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、付费专栏及课程。

余额充值