且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义View仿TabHost的实现

更新时间:2022-09-09 20:51:45

Activity1如下:

[java] view plaincopy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity1 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity1");  
  11.         setContentView(R.layout.activity_1);  
  12.     }  
  13. }  


Activity2如下:

[java] view plaincopy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity2 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity2");  
  11.         setContentView(R.layout.activity_2);  
  12.     }  
  13. }  


Activity3如下:

[java] view plaincopy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity3 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity3");  
  11.         setContentView(R.layout.activity_3);  
  12.     }  
  13. }  


Activity4如下:

[java] view plaincopy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. public class Activity4 extends Activity{  
  5.      @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         //设置setTitle应该在setTitle之前  
  9.         //否则不起作用  
  10.         setTitle("Activity4");  
  11.         setContentView(R.layout.activity_4);  
  12.     }  
  13. }  


BottomMenuView如下:

[java] view plaincopy
  1. package cn.com;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.util.AttributeSet;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.LinearLayout;  
  10. //自定义View.仿TabHost的实现  
  11. public class BottomMenuView extends LinearLayout{  
  12.     private Context mContext;  
  13.     private LinearLayout mLinearLayout1;  
  14.     private LinearLayout mLinearLayout2;  
  15.     private LinearLayout mLinearLayout3;  
  16.     private LinearLayout mLinearLayout4;  
  17.     private String mCurrentActivityTitle;  
  18.     private final static String ACTIVITY_NAME_1="Activity1";  
  19.     private final static String ACTIVITY_NAME_2="Activity2";  
  20.     private final static String ACTIVITY_NAME_3="Activity3";  
  21.     private final static String ACTIVITY_NAME_4="Activity4";  
  22.     public BottomMenuView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.         mContext=context;  
  25.         initView();  
  26.     }  
  27.     private void initView(){  
  28.         View bottomMenuView=  
  29.         LayoutInflater.from(mContext).inflate(R.layout.bottommenu, nullfalse);  
  30.         ViewGroup.LayoutParams params=  
  31.         new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);  
  32.         this.addView(bottomMenuView,params);  
  33.           
  34.         mLinearLayout1=(LinearLayout)  
  35.         bottomMenuView.findViewById(R.id.menu_LinearLayout1);  
  36.         mLinearLayout2=(LinearLayout)  
  37.         bottomMenuView.findViewById(R.id.menu_LinearLayout2);  
  38.         mLinearLayout3=(LinearLayout)  
  39.         bottomMenuView.findViewById(R.id.menu_LinearLayout3);  
  40.         mLinearLayout4=(LinearLayout)  
  41.         bottomMenuView.findViewById(R.id.menu_LinearLayout4);  
  42.           
  43.         BottomMenuOnClickListener clickListener=new BottomMenuOnClickListener();  
  44.         mLinearLayout1.setOnClickListener(clickListener);  
  45.         mLinearLayout2.setOnClickListener(clickListener);  
  46.         mLinearLayout3.setOnClickListener(clickListener);  
  47.         mLinearLayout4.setOnClickListener(clickListener);  
  48.         setEveryMenuStatus();  
  49.     }  
  50.       
  51.     /** 
  52.      *  
  53.      * 1 在每个menu对应的Activity中设置其Title 
  54.      * 2 在此判断当前显示是哪一个Activity.使得 
  55.      *   当前Activity对应的Menu不可再次点击. 
  56.      */  
  57.     private void setEveryMenuStatus(){  
  58.         mCurrentActivityTitle=(String) ((Activity)mContext).getTitle();  
  59.         if (mCurrentActivityTitle.equals(ACTIVITY_NAME_1)) {  
  60.             mLinearLayout1.setClickable(false);  
  61.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_2)) {  
  62.             mLinearLayout2.setClickable(false);  
  63.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_3)) {  
  64.             mLinearLayout3.setClickable(false);  
  65.         }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_4)) {  
  66.             mLinearLayout4.setClickable(false);  
  67.         }  
  68.     }  
  69.       
  70.     private class BottomMenuOnClickListener implements OnClickListener{  
  71.         public void onClick(View v) {  
  72.             switch (v.getId()) {  
  73.             case R.id.menu_LinearLayout1:  
  74.                 Intent intent1=new Intent(mContext, Activity1.class);  
  75.                 mContext.startActivity(intent1);  
  76.                 break;  
  77.             case R.id.menu_LinearLayout2:  
  78.                 Intent intent2=new Intent(mContext, Activity2.class);  
  79.                 mContext.startActivity(intent2);  
  80.                 break;  
  81.             case R.id.menu_LinearLayout3:  
  82.                 Intent intent3=new Intent(mContext, Activity3.class);  
  83.                 mContext.startActivity(intent3);  
  84.                 break;  
  85.             case R.id.menu_LinearLayout4:  
  86.                 Intent intent4=new Intent(mContext, Activity4.class);  
  87.                 mContext.startActivity(intent4);  
  88.                 break;  
  89.             default:  
  90.                 break;  
  91.             }  
  92.               
  93.         }  
  94.           
  95.     }  
  96.   
  97. }  

activity_1.xml如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 1"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


activity_2.xml如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 2"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  

 

activity_3.xml如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 3"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


activity_4.xml如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.       
  7.     <TextView   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="Activity 4"  
  12.         android:textSize="50sp"  
  13.     />  
  14.       <cn.com.BottomMenuView  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="70dip"  
  17.         android:layout_alignParentBottom="true"  
  18.      />  
  19. </RelativeLayout>  


bottommenu.xml如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="70dip"  
  5.     android:orientation="horizontal" >  
  6.     <LinearLayout   
  7.         android:id="@+id/menu_LinearLayout1"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:clickable="true"  
  11.         android:background="@drawable/photo1"  
  12.         android:layout_weight="1"  
  13.     />  
  14.     <LinearLayout   
  15.         android:id="@+id/menu_LinearLayout2"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:clickable="true"  
  19.         android:background="@drawable/photo2"  
  20.         android:layout_weight="1"  
  21.     />  
  22.     <LinearLayout   
  23.         android:id="@+id/menu_LinearLayout3"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:clickable="true"  
  27.         android:background="@drawable/photo3"  
  28.         android:layout_weight="1"  
  29.     />  
  30.     <LinearLayout   
  31.         android:id="@+id/menu_LinearLayout4"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:clickable="true"  
  35.         android:background="@drawable/photo4"  
  36.         android:layout_weight="1"  
  37.     />  
  38.       
  39. </LinearLayout>  


manifest.xml如下:

[html] view plaincopy
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="cn.com"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.          <activity  
  15.             android:name=".Activity1">  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.          </activity>  
  21.           <activity  
  22.             android:name=".Activity2">  
  23.          </activity>  
  24.           <activity  
  25.             android:name=".Activity3">  
  26.          </activity>  
  27.           <activity  
  28.             android:name=".Activity4">  
  29.          </activity>  
  30.     </application>  
  31.   
  32. </manifest>