tabhost_tabwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- <FrameLayout
android:id="@+id/real_tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"/> -->
</LinearLayout>
</TabHost>
package com.stone.ui;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import com.stone.R;
/*
* ____________
* | |
* | |
* |tabcontent|
* |显示标签页 |
* |__________| 区域是由tabhost控制, 显示内容的区域为tabcontent
* tab标签 组合 fragment
*/
public class TestTabWidget extends Activity {
TabHost tabHost;
Fragment frag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost_tabwidget);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
System.out.println("current tabid=" + tabId);
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (TextUtils.equals("first", tabId)) {
//add/replace fragment first
frag = new Frag1();
System.out.println("load f1");
} else if (TextUtils.equals("second", tabId)) {
//add/replace fragment second
frag = new Frag2();
System.out.println("load f2");
} else if (TextUtils.equals("third", tabId)) {
//add/replace fragment third
frag = new Frag3();
System.out.println("load f3");
}
ft.replace(android.R.id.tabcontent, frag, "frag");
ft.commit();
}
});
tabHost.addTab(tabHost.newTabSpec("first").setIndicator("First")
.setContent(new DummyTabFactory(this)));
tabHost.addTab(tabHost.newTabSpec("second").setIndicator("Second")//setIndicator 设置标签样式
.setContent(new DummyTabFactory(this))); //setContent 点击标签后触发
tabHost.addTab(tabHost.newTabSpec("third").setIndicator("Third")
.setContent(new DummyTabFactory(this)));
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private Context context;
public DummyTabFactory(Context ctx) {
this.context = ctx;
}
@Override
public View createTabContent(String tag) {//创建宽高均为0的view
View v = new ImageView(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
}
效果
分享到:
相关推荐
该应用利用Tabhost+Fragment实现Android框架。
android:TabHost+Fragment实现Tab切换
TabHostActivity在API 13之后就不能用了。 写了个Demo,用FragmentActivity + Tabhost实现多标签页,并添加滑动手势。 API=19,可运行
Android底部导航栏——FragmentTabHost+ViewPager+Fragment
android实现可滑动标签页有很多方法,这里给大家介绍一种官方推荐,性能最高,并且代码最少的方式,就是利用FragmentTabhost+ViewPager+FragmentPagerAdapter来实现。
自Android3.2之后,TabActibvity被弃用(Deprecated),取而代之的是FragmentActivity,因为Fragment比Activiy更灵活,消耗的资源更小,完全能够满足TabActivity的效果,所以直接替代之。原来的TabActibvity+TabHost+...
主要介绍了Android中使用TabHost 与 Fragment 制作页面切换效果的相关资料,需要的朋友可以参考下
利用Tabhost + Fragment实现android标签切换界面.
Fragment + TabHost + RadioGroup 结合DEMO||Fragment + TabHost + RadioGroup 结合DEMO
用Fragment 与 TabHost 实现 Fragment 的切换
本demo主要是用Fragment实现TabHost的功能,是完全的实现,和之前两个demo相比,这个是完全从不同的角度来实现选项卡的功能,可以看看这三篇文章,对这个demo的理解会更深刻一些...
自定义一个HorizontalScrollView实现左右横向滑动 使用viewpager+fragment合用,开发类似tabhost效果
模仿新浪微博底部導航欄,使用RadioGroup、TabHost、Fragment實現
利用ViewPager + TabHost + Fragment实现android标签切换界面.
Android fragment 内部嵌套tabhost组件 详情请见: http://blog.csdn.net/singleton1900/article/details/27087833
– TabHost+Fragment – RadioGroup+Fragment – FragmentTabHost+Fragment – 5.0以后有个新控件,BottomNavigator 这篇主要介绍下FragmentTabHost配合Fragment使用 运行效果图 效果图分析 FragmentTabHost...
使用FragmentTabHost+ Fragment实现常见的布局
android中fragment实现tabhost标签切换效果
用viewpager和fragment配合实现以前的tabhost效果 可以滑动更换内容 更加帅气 附带左拉菜单效果 需要的sliding包也在里面 还测试了fragment与activity的通讯