自定義View仿TabHost的實現
Activity1如下:
- package cn.com;
- import android.app.Activity;
- import android.os.Bundle;
- public class Activity1 extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //設置setTitle應該在setTitle之前
- //否則不起作用
- setTitle("Activity1");
- setContentView(R.layout.activity_1);
- }
- }
Activity2如下:
- package cn.com;
- import android.app.Activity;
- import android.os.Bundle;
- public class Activity2 extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //設置setTitle應該在setTitle之前
- //否則不起作用
- setTitle("Activity2");
- setContentView(R.layout.activity_2);
- }
- }
Activity3如下:
- package cn.com;
- import android.app.Activity;
- import android.os.Bundle;
- public class Activity3 extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //設置setTitle應該在setTitle之前
- //否則不起作用
- setTitle("Activity3");
- setContentView(R.layout.activity_3);
- }
- }
Activity4如下:
- package cn.com;
- import android.app.Activity;
- import android.os.Bundle;
- public class Activity4 extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //設置setTitle應該在setTitle之前
- //否則不起作用
- setTitle("Activity4");
- setContentView(R.layout.activity_4);
- }
- }
BottomMenuView如下:
- package cn.com;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- //自定義View.仿TabHost的實現
- public class BottomMenuView extends LinearLayout{
- private Context mContext;
- private LinearLayout mLinearLayout1;
- private LinearLayout mLinearLayout2;
- private LinearLayout mLinearLayout3;
- private LinearLayout mLinearLayout4;
- private String mCurrentActivityTitle;
- private final static String ACTIVITY_NAME_1="Activity1";
- private final static String ACTIVITY_NAME_2="Activity2";
- private final static String ACTIVITY_NAME_3="Activity3";
- private final static String ACTIVITY_NAME_4="Activity4";
- public BottomMenuView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext=context;
- initView();
- }
- private void initView(){
- View bottomMenuView=
- LayoutInflater.from(mContext).inflate(R.layout.bottommenu, null, false);
- ViewGroup.LayoutParams params=
- new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
- this.addView(bottomMenuView,params);
- mLinearLayout1=(LinearLayout)
- bottomMenuView.findViewById(R.id.menu_LinearLayout1);
- mLinearLayout2=(LinearLayout)
- bottomMenuView.findViewById(R.id.menu_LinearLayout2);
- mLinearLayout3=(LinearLayout)
- bottomMenuView.findViewById(R.id.menu_LinearLayout3);
- mLinearLayout4=(LinearLayout)
- bottomMenuView.findViewById(R.id.menu_LinearLayout4);
- BottomMenuOnClickListener clickListener=new BottomMenuOnClickListener();
- mLinearLayout1.setOnClickListener(clickListener);
- mLinearLayout2.setOnClickListener(clickListener);
- mLinearLayout3.setOnClickListener(clickListener);
- mLinearLayout4.setOnClickListener(clickListener);
- setEveryMenuStatus();
- }
- /**
- *
- * 1 在每個menu對應的Activity中設置其Title
- * 2 在此判斷當前顯示是哪一個Activity.使得
- * 當前Activity對應的Menu不可再次點擊.
- */
- private void setEveryMenuStatus(){
- mCurrentActivityTitle=(String) ((Activity)mContext).getTitle();
- if (mCurrentActivityTitle.equals(ACTIVITY_NAME_1)) {
- mLinearLayout1.setClickable(false);
- }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_2)) {
- mLinearLayout2.setClickable(false);
- }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_3)) {
- mLinearLayout3.setClickable(false);
- }else if (mCurrentActivityTitle.equals(ACTIVITY_NAME_4)) {
- mLinearLayout4.setClickable(false);
- }
- }
- private class BottomMenuOnClickListener implements OnClickListener{
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.menu_LinearLayout1:
- Intent intent1=new Intent(mContext, Activity1.class);
- mContext.startActivity(intent1);
- break;
- case R.id.menu_LinearLayout2:
- Intent intent2=new Intent(mContext, Activity2.class);
- mContext.startActivity(intent2);
- break;
- case R.id.menu_LinearLayout3:
- Intent intent3=new Intent(mContext, Activity3.class);
- mContext.startActivity(intent3);
- break;
- case R.id.menu_LinearLayout4:
- Intent intent4=new Intent(mContext, Activity4.class);
- mContext.startActivity(intent4);
- break;
- default:
- break;
- }
- }
- }
- }
activity_1.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Activity 1"
- android:textSize="50sp"
- />
- <cn.com.BottomMenuView
- android:layout_width="fill_parent"
- android:layout_height="70dip"
- android:layout_alignParentBottom="true"
- />
- </RelativeLayout>
activity_2.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Activity 2"
- android:textSize="50sp"
- />
- <cn.com.BottomMenuView
- android:layout_width="fill_parent"
- android:layout_height="70dip"
- android:layout_alignParentBottom="true"
- />
- </RelativeLayout>
activity_3.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Activity 3"
- android:textSize="50sp"
- />
- <cn.com.BottomMenuView
- android:layout_width="fill_parent"
- android:layout_height="70dip"
- android:layout_alignParentBottom="true"
- />
- </RelativeLayout>
activity_4.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Activity 4"
- android:textSize="50sp"
- />
- <cn.com.BottomMenuView
- android:layout_width="fill_parent"
- android:layout_height="70dip"
- android:layout_alignParentBottom="true"
- />
- </RelativeLayout>
bottommenu.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="70dip"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/menu_LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:background="@drawable/photo1"
- android:layout_weight="1"
- />
- <LinearLayout
- android:id="@+id/menu_LinearLayout2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:background="@drawable/photo2"
- android:layout_weight="1"
- />
- <LinearLayout
- android:id="@+id/menu_LinearLayout3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:background="@drawable/photo3"
- android:layout_weight="1"
- />
- <LinearLayout
- android:id="@+id/menu_LinearLayout4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:clickable="true"
- android:background="@drawable/photo4"
- android:layout_weight="1"
- />
- </LinearLayout>
manifest.xml如下:
- <manifest xmlns:android="https://schemas.android.com/apk/res/android"
- package="cn.com"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".Activity1">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".Activity2">
- </activity>
- <activity
- android:name=".Activity3">
- </activity>
- <activity
- android:name=".Activity4">
- </activity>
- </application>
- </manifest>
最後更新:2017-04-04 07:03:15