FragmentTabHost( 选 项卡)
来源:互联网 发布:java批量发送邮件 编辑:程序博客网 时间:2024/05/20 10:52
现在市面上app的主流框架大体分为两种: 一种是在主界面点击菜单按钮, 之后会滑出侧滑菜单, 之后进入到各个模块, 还有一种是
在主界面的下面放置若干个tab按钮, 点击按钮, 切换到不同的模块。 今天要讲的就是第二种的实现方式之一的FragmentTabHost.( 选
项卡)
在主界面的下面放置若干个tab按钮, 点击按钮, 切换到不同的模块。 今天要讲的就是第二种的实现方式之一的FragmentTabHost.( 选
项卡)
(实例)
1.activity_main.xml
<FrameLayout
android:id="@+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
</android.support.v4.app.FragmentTabHost>
<FrameLayout
android:id="@+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
</android.support.v4.app.FragmentTabHost>
2.tab.xml
<LinearLayout
android:id="@+id/ll_tab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/tab_bg"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/tab_bg"
android:src="@mipmap/nav_icon_home_sel" />
<TextView
android:id="@+id/tab_title"
style="@style/CustomTextViewStyle03"
android:layout_marginTop="3dp"
android:text="首页" />
<LinearLayout
android:id="@+id/ll_tab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/tab_bg"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/tab_bg"
android:src="@mipmap/nav_icon_home_sel" />
<TextView
android:id="@+id/tab_title"
style="@style/CustomTextViewStyle03"
android:layout_marginTop="3dp"
android:text="首页" />
3,Activity中使用如下:
/**
* tabhost选项卡
*/
private TabWidget mTabWidget;
private FragmentTabHost mTabHost;
/**
* 要加载所有的Fragment
*/
private Class<?>[] mFragments = {HomeFragment.class, OrderFragment.class, SelectCarFragment.class, CustomerFragment.class,
MineFragment.class,};
private String[] titles = newString[]{"首页", "订单", "底价购车", "客服", "我的"};
mTabHost = findViewByIdNoCast(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.layout_frame);
//获取一个TabWiidget
mTabWidget = mTabHost.getTabWidget();
mTabWidget.setDividerDrawable(null);
//为tabHost添加fragment
int menuCount = mFragments.length;
for (int i = 0; i < menuCount; i++) {
TabHost.TabSpec spec = mTabHost.newTabSpec(titles[i]).setIndicator(getItemView(i));
mTabHost.addTab(spec, mFragments[i], null);
} priv
ate View getItemView(int index) {
View view = LayoutInflater.from(this).inflate(R.layout.tab, null);
TextView tv = (TextView) view.findViewById(R.id.tab_title);
ImageView icon = (ImageView) view.findViewById(R.id.tab_icon);
return view;
} //
为FragmentTabHost添加点击事件
mTabHost.setOnTabChangedListener(newTabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabName) {
for (int i = 0; i < mTabWidget.getTabCount(); i++) {
View view = mTabWidget.getChildTabViewAt(i);
TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
ImageView tab_icon = (ImageView) view.findViewById(R.id.tab_icon);
if (i == mTabHost.getCurrentTab()) {
current_tab = i;
tab_icon.setImageResource(iconS[i]);
tab_title.setTextColor(Color.parseColor("#ffff4b14"));
} else {
tab_title.setTextColor(Color.rgb(162, 172, 181));
tab_icon.setImageResource(iconN[i]);
}
}
}
});
/**
* tabhost选项卡
*/
private TabWidget mTabWidget;
private FragmentTabHost mTabHost;
/**
* 要加载所有的Fragment
*/
private Class<?>[] mFragments = {HomeFragment.class, OrderFragment.class, SelectCarFragment.class, CustomerFragment.class,
MineFragment.class,};
private String[] titles = newString[]{"首页", "订单", "底价购车", "客服", "我的"};
mTabHost = findViewByIdNoCast(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.layout_frame);
//获取一个TabWiidget
mTabWidget = mTabHost.getTabWidget();
mTabWidget.setDividerDrawable(null);
//为tabHost添加fragment
int menuCount = mFragments.length;
for (int i = 0; i < menuCount; i++) {
TabHost.TabSpec spec = mTabHost.newTabSpec(titles[i]).setIndicator(getItemView(i));
mTabHost.addTab(spec, mFragments[i], null);
} priv
ate View getItemView(int index) {
View view = LayoutInflater.from(this).inflate(R.layout.tab, null);
TextView tv = (TextView) view.findViewById(R.id.tab_title);
ImageView icon = (ImageView) view.findViewById(R.id.tab_icon);
return view;
} //
为FragmentTabHost添加点击事件
mTabHost.setOnTabChangedListener(newTabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabName) {
for (int i = 0; i < mTabWidget.getTabCount(); i++) {
View view = mTabWidget.getChildTabViewAt(i);
TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
ImageView tab_icon = (ImageView) view.findViewById(R.id.tab_icon);
if (i == mTabHost.getCurrentTab()) {
current_tab = i;
tab_icon.setImageResource(iconS[i]);
tab_title.setTextColor(Color.parseColor("#ffff4b14"));
} else {
tab_title.setTextColor(Color.rgb(162, 172, 181));
tab_icon.setImageResource(iconN[i]);
}
}
}
});
自写的方式第二种
XML
注意布局文件当中使用的名字必要是系统定义的名字不能是其他的名字
Acitivity当中使用
public class MainActivity extends AppCompatActivity implements TabHost.OnTabChangeListener {
private FragmentTabHost tabHost;
//布局文件
private Class[] fragment = new Class[]{Fragment01.class, Fragment02.class, Fragment03.class, Fragment04.class};
//文字信息 这里当然也可以添加其他信息比如图片之类
private String[] String1 = new String[]{"小学", "初中", "高中", "大学"};
private TabWidget tabWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
//初始化数据
tabHost.setup(this, getSupportFragmentManager(), R.id.action0_fragment01);
//划线的
tabWidget = tabHost.getTabWidget();
tabWidget.setDividerDrawable(null);
for (int i = 0; i < fragment.length; i++) {
TabHost.TabSpec spec = tabHost.newTabSpec(String1[i]).setIndicator(getview(i));
tabHost.addTab(spec, fragment[i], null);}
tabHost.setOnTabChangedListener(this);}
public View getview(int pos) {
//自定义布局文件显示选项卡里面的内容
View view = View.inflate(this, R.layout.demo, null);
TextView sette = (TextView) view.findViewById(R.id.sdgdfgsrgsregsdrh);
//传递位置信息到触摸事件当中去
sette.setText(String1[pos]);
if (pos == 0) {sette.setTextColor(Color.parseColor("#265458"));
} else {sette.setTextColor(Color.parseColor("#fff000"));}
return view;}
@Override
public void onTabChanged(String s) {
for (int i = 0; i < tabWidget.getTabCount(); i++) {
//通过反射拿到自布局
View view = tabWidget.getChildTabViewAt(i);
TextView sette = (TextView) view.findViewById(R.id.sdgdfgsrgsregsdrh);
if (i == tabHost.getCurrentTab()) {
sette.setTextColor(Color.parseColor("#265458"));
} else {
sette.setTextColor(Color.parseColor("#fff000"));}
}
}
0 0
- FragmentTabHost( 选 项卡)
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabHost
- FragmentTabhost
- FragmentTabHost
- <Android 基础(三十八)> FragmentTabHost
- tabhost底部分显示(FragmentTabHost)
- Android FragmentTabHost嵌套(ViewPager+Fragment)问题
- FragmentTabHost的使用(上面切换)
- Android (FragmentTabHost+RadioGroup)实现底部bar
- 网关,路由,局域网内的通信及不同的网络间通信实现的原理剖析
- KMP算法
- 虚拟机连接VPN
- 实战学习NodeJS建站(3)—bootstrap后台管理、nodejs访问数据库
- Kindle 的一些网站
- FragmentTabHost( 选 项卡)
- 【Hibernate】---- 数据缓存
- 动态规划入门——HUDU 2084 数塔
- 更新自己编译android源码遇到的问题
- getRawX、getRawY与getX、getY以及View中的getScrollX、getScrollY
- Python 2.7 执行某一程序并获取PID
- PHP 连接 MySQL(MySQLi方式)
- Item12 Declare overriding function override
- NPOI获取图片详细信息