android使用继承的方式实现底部菜单栏

去繁从简,记录学习中的点点滴滴


实现过程:

1,、在父类(BaseActivity)中写一个方法initTab(),在其中找到每个tab的id,并绑定监听

	public Button mButton_main;
	public Button mButton_page1;
	public Button mButton_page2;
	public Button mButton_page3;
	public Intent intent;
	
	public void initTab() {
		intent = new Intent();
		mButton_main = (Button) findViewById(R.id.tab_1);
		mButton_page1 = (Button) findViewById(R.id.tab_2);
		mButton_page2 = (Button) findViewById(R.id.tab_3);
		mButton_page3 = (Button) findViewById(R.id.tab_4);
		mButton_main.setOnClickListener(this);
		mButton_page1.setOnClickListener(this);
		mButton_page2.setOnClickListener(this);
		mButton_page3.setOnClickListener(this);
	}


2、父类实现OnClickListener接口,并在OnClik中定义好每个tab要响应的事件(跳转、finish当前的Activity)

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab_1:
			Toast.makeText(getApplicationContext(), "tab1", 1000).show();
			if (!(this instanceof MainActivity)) {
				intent.setClass(this, MainActivity.class);
				startActivity(intent);
				this.finish();
			}
			break;
		case R.id.tab_2:
			Toast.makeText(getApplicationContext(), "tab2", 1000).show();
			if (!(this instanceof Page1Activity)) {
				intent.setClass(this, Page1Activity.class);
				startActivity(intent);
				this.finish();
			}
			break;
		case R.id.tab_3:
			Toast.makeText(getApplicationContext(), "tab3", 1000).show();
			if (!(this instanceof Page2Activity)) {
				intent.setClass(this, Page2Activity.class);
				startActivity(intent);
				this.finish();
			}
			break;
		case R.id.tab_4:
			Toast.makeText(getApplicationContext(), "tab4", 1000).show();
			if (!(this instanceof Page3Activity)) {
				intent.setClass(this, Page3Activity.class);
				startActivity(intent);
				this.finish();
			}
			break;

		}

3、把各个tab定义到一个LinearLayout或RelativeLayout中,并添加在每一个子类(要用到tab的Activity)的布局中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="第二页" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/tab_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/tab_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/tab_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/tab_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

4,、在子类的oncreate中调用initTab()

public class MainActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initTab();
	}

}

注:1、每个tab可以是一个button也可以是一个RelativeLayout,大大增加对tab的操作,维护或修改也很方便,只要在BaseActivity中找到空间并绑定监听事件和在对应的布局中添加相应的tab就可以了

2、跳转时要排除跳转到自己这个Activity的情况

3,、可以添加activity跳转之间的动画效果

源码下载:http://download.csdn.net/detail/u012895758/6580347

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值