主流移动应用开发框架(2)——fragment+fragmenttabhost实现底部选项卡导航(可滑动切换)

Fragment对于我们来说可能并不陌生,在android3.0之后引进开发,对于处理平板大屏幕界面分布,fragment有着activity没有的优势,它“寄生”于activity解决了一个屏幕显示多个“分屏”的问题,管理同一个activity下多个“碎片”界面的布局显示及其数据交互。在3.0版本以下的开发环境,则需要导入V4到作为支持。

fragment的具体使用方法在这里并没有详细介绍,而主要要介绍是底部选项卡导航的实现,我们先看看效果图

实现参考了github上面的一些例子和网友的一些demo,附上自己的demo下载链接 点击打开链接

主界面代码

package com.example.fragmenttabhost;

import com.example.fragmentbottommenu.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 
 * @author yummy
 * email:[email protected]
 * 主菜单界面
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		Button nosupport = (Button) findViewById(R.id.nosupport);
		Button support = (Button) findViewById(R.id.support);
		nosupport.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, FragmentTab.class);
                 startActivity(intent);
			}
		});

		support.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent intent = new Intent(MainActivity.this, FragmentTabSupportSlip.class);
				startActivity(intent);
			}
		});
	}
}

主界面布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_centerInParent="true"
        android:id="@+id/nosupport"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="不支持滑动的菜单"/>

    <Button
        android:layout_below="@+id/nosupport"
        android:id="@+id/support"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="支持滑动的菜单" />
    
</RelativeLayout>


其中跳转到两个activity,一个是不支持滑动FragmentTab.java,一个是支持滑动FragmentTabSupportSlip.java

FragmentTab.java代码

package com.example.fragmenttabhost;

import com.example
  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值