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>
FragmentTab.java代码
package com.example.fragmenttabhost;
import com.example