<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@+id/tabwidget_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/layout_container_tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.app.FragmentTabHost>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.widget.TabHost.TabSpec;
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
private Bundle bundle1, bundle2, bundle3, bundle4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
FragmentManager manager = getSupportFragmentManager();
tabHost.setup(this, manager, R.id.layout_container_tabcontent);
TabSpec tabSpec1 = tabHost.newTabSpec("records");
tabSpec1.setIndicator("记录");
bundle1 = new Bundle();
bundle1.putInt("tabIndex", 1);
TabSpec tabSpec2 = tabHost.newTabSpec("contacts");
tabSpec2.setIndicator("联系人");
bundle2 = new Bundle();
bundle2.putInt("tabIndex", 2);
TabSpec tabSpec3 = tabHost.newTabSpec("collections");
tabSpec3.setIndicator("收藏夹");
bundle3 = new Bundle();
bundle3.putInt("tabIndex", 3);
TabSpec tabSpec4 = tabHost.newTabSpec("groups");
tabSpec4.setIndicator("群组");
bundle4 = new Bundle();
bundle4.putInt("tabIndex", 4);
tabHost.addTab(tabSpec1, DummyFragment.class, bundle1);
tabHost.addTab(tabSpec2, DummyFragment.class, bundle2);
tabHost.addTab(tabSpec3, DummyFragment.class, bundle3);
tabHost.addTab(tabSpec4, DummyFragment.class, bundle4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DummyFragment extends Fragment {
private TextView text_dummyfragment;
private Bundle bundle = null;
private int tabIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = getArguments();
tabIndex = bundle.getInt("tabIndex");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dummy, container, false);
text_dummyfragment = (TextView) view
.findViewById(R.id.text_dummyfragment);
Log.i("", "==" + tabIndex);
switch (tabIndex) {
case 1:
text_dummyfragment.setText("这个是记录页面");
break;
case 2:
text_dummyfragment.setText("这个是联系人页面");
break;
case 3:
text_dummyfragment.setText("这个是收藏夹页面");
break;
case 4:
text_dummyfragment.setText("这个是群组页面");
break;
default:
break;
}
return view;
}
}
分享到:
相关推荐
fragmenttabhost 使用fragmenttabhost 使用fragmenttabhost 使用fragmenttabhost 使用fragmenttabhost 使用
tabhost在API13以后就不用了呢,fragmentTabhost是它的替换类
FragmentTabHost使用简单示例。
FragmentTabHost
FragmentTabHost,一个很常用的控件。初次接触时,在GOOGLE上找了一些博客,看了些例子,被复杂的结构和很多没接触过的函数弄晕了头。所以自己尝试在那些例子上简化一些,只做FragmentTabHost 最基本的界面功能
android FragmentTabHost使用的案例,案例使用的是V4包下的FragmentTabHost
FragmentTabhost实现android底部导航
查看了FragmentTabHost的源码,可以知道FragmentTabHost不保存状态是因为切换fragment的时候是使用detach和attach来Fragment的隐藏和显示的,这样的话每次切换肯定要重新加载布局,处理使用detach和attach,我们还...
FragmentTabHost实现中间按钮凸出效果
Tabhost+Activity的模式过时了,现在用FragmentActivity+FragmentTabhost自动帮我们管理 每个Page,
重写FragmentTabHost,防止FragmentTabHost切换fragment重新调用onCreateView(),博客中有说明
FragmentTabHost Fragment 状态保存
1.如何使用FragmentTabHost进行布局 2.Fragment如何使用
启动页面停留3秒利用的是Handler() 引导页面用的是viewpager ,fragmenttabhost用的是 V4 包中的,俗称片段。
FragmentTabHost常见使用方法,http://blog.csdn.net/yalinfendou博客中有详细介绍……
FragmentTabHost示例化代码,通过布局文件可以把Tab放在最上面或最下面
FragmentTabHost动态加载fragment的TAB页签,提高响应速度,类activityhost的另一种实现方式