请教各位大神一些关于fragmenttabhost底部 的问题

Android常用控件之FragmentTabHost的使用
& &最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到FragmentActivity上。tab可以放在最上面也可以放在最下面
由以下布局文件main.xml&FrameLayout&的位置决定
&?xml version=&1.0& encoding=&utf-8&?& &
&LinearLayout xmlns:android=&/apk/res/android& &
& & android:layout_width=&match_parent& &
& & android:layout_height=&match_parent& &
& & android:orientation=&vertical&& &
& & &!-- 这个布局决定了标签在上面还是在下面显示 --& &
& & &FrameLayout &&
& & & & android:id=&@+id/realtabcontent& &
& & & & android:layout_width=&match_parent& &
& & & & android:layout_height=&0dip& &
& & & & android:layout_weight=&1& /& &
& & &android.support.v4.app.FragmentTabHost &
& & & & android:id=&@android:id/tabhost& &
& & & & android:layout_width=&match_parent& &
& & & & android:layout_height=&wrap_content&& &
& & & & &&
& & & & &TabWidget &&
& & & & & & android:id=&@android:id/tabs& &
& & & & & & android:layout_width=&match_parent& &
& & & & & & android:layout_height=&wrap_content& &
& & & & & & android:orientation=&horizontal&/& &
& & &/android.support.v4.app.FragmentTabHost& &
&/LinearLayout& &
创建一个类继承FragmentActivity
package com.example. &
import android.os.B &
import android.support.v4.app.FragmentA &
import android.support.v4.app.FragmentTabH &
import android.view.V &
import android.widget.TextV &
import com.example.tabhost.FirstF &
import com.example.tabhost.ThirdF &
import com.example.tabhost.secondF &
public class MainActivity extends FragmentActivity { &
& & private FragmentTabHost mTabHost =; &
& & private View indicator = &
& & @Override &
& & protected void onCreate(Bundle savedInstanceState) { &
& & & & super.onCreate(savedInstanceState); &
& & & & setContentView(R.layout.main); &
& & & & mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); &
& & & & mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); &
& & & & // 添加tab名称和图标 &
& & & & indicator = getIndicatorView(&我的联系人&, R.layout.mycontact_indicator); &
& & & & mTabHost.addTab(mTabHost.newTabSpec(&myContact&) &
& & & & & & & & .setIndicator(indicator), FirstFragment.class, null); &
& & & & indicator = getIndicatorView(&陌生人&, R.layout.strangercontact_indicator); &
& & & & mTabHost.addTab( &
& & & & & & & & mTabHost.newTabSpec(&stranger&).setIndicator(indicator), &
& & & & & & & & secondFragment.class, null); &
& & & & indicator = getIndicatorView(&常联系人&, R.layout.alwayscontact_indicator); &
& & & & mTabHost.addTab( &
& & & & & & & & mTabHost.newTabSpec(&alwaysContact&).setIndicator(indicator), &
& & & & & & & & ThirdFragment.class, null); &
& & private View getIndicatorView(String name, int layoutId) { &
& & & & View v = getLayoutInflater().inflate(layoutId, null); &
& & & & TextView tv = (TextView) v.findViewById(R.id.tabText); &
& & & & tv.setText(name); &
& & @Override &
& & protected void onDestroy() { &
& & & & // TODO Auto-generated method stub &
& & & & super.onDestroy(); &
& & & & mTabHost = &
第一个Tab的布局文件 & &存放两张图片,字体颜色
alwayscontact_indicator.xml文件
[html] view plaincopy
&?xml version=&1.0& encoding=&utf-8&?& &
&LinearLayout xmlns:android=&/apk/res/android& &
& & android:layout_width=&match_parent& &
& & android:layout_height=&match_parent& &
& & android:layout_gravity=&center& &&
& & android:gravity=&center&& &
& & &TextView &&
& & & & android:id=&@+id/tabText& &
& & & & android:layout_width=&wrap_content& &
& & & & android:layout_height=&wrap_content& &
& & & & android:focusable=&true& &
& & & & android:drawableTop=&@drawable/mycontact_selector& &
& & & & android:textColor=&@drawable/tabitem_txt_sel&/& &
&/LinearLayout& &
mycontact_selector.xml文件
&?xml version=&1.0& encoding=&utf-8&?& &
&selector xmlns:android=&/apk/res/android& & & & &
& & &!-- Non focused states --& &
& & &item android:state_focused=&false& android:state_selected=&false& android:state_pressed=&false& android:drawable=&@drawable/mycontact& /& &
& & &item android:state_focused=&false& android:state_selected=&true& android:state_pressed=&false& android:drawable=&@drawable/mycontact_sel& /& &
& & &!-- Focused states --& &
& & &item android:state_focused=&true& android:state_selected=&false& android:state_pressed=&false& android:drawable=&@drawable/mycontact_sel& /& &
& & &item android:state_focused=&true& android:state_selected=&true& android:state_pressed=&false& android:drawable=&@drawable/mycontact_sel& /& &
& & &!-- Pressed --& &
& & &item android:state_selected=&true& android:state_pressed=&true& android:drawable=&@drawable/mycontact_sel& /& &
& & &item android:state_pressed=&true& android:drawable=&@drawable/mycontact_sel& /& & &
&/selector& &
tabitem_txt_sel.xml文件
&?xml version=&1.0& encoding=&utf-8&?& &
&selector xmlns:android=&/apk/res/android& & & & &
& & &!-- Non focused states --& &
& & &item android:state_focused=&false& android:state_selected=&false& android:state_pressed=&false& android:color=&#A4A4A4& /& &
& & &item android:state_focused=&false& android:state_selected=&true& android:state_pressed=&false& android:color=&#00A3F5& /& &
& & &!-- Focused states --& &
& & &item android:state_focused=&true& android:state_selected=&false& android:state_pressed=&false& android:color=&#00A3F5& /& &
& & &item android:state_focused=&true& android:state_selected=&true& android:state_pressed=&false& android:color=&#00A3F5& /& &
& & &!-- Pressed --& &
& & &item android:state_selected=&true& android:state_pressed=&true& android:color=&#00A3F5& /& &
& & &item android:state_pressed=&true& android:color=&#00A3F5& /& &&
&/selector& &
其它的tab文件定义也是类似的,看下最后的效果图
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。当前访客身份:游客 [
当前位置:
public class TestSetupTypeSettings extends Fragment implements OnItemClickListener,
TabHost.TabContentFactory, TabHost.OnTabChangeListener {
private int mCurrentT
private TabHost mTabH
private static final String TAG = &TestSetupTypeSettings&;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// super.onCreate(savedInstanceState);
View tablayout = inflater.inflate(R.layout.projects, container, false);
mTabHost = (TabHost) tablayout.findViewById(android.R.id.tabhost);
final View v1 = tablayout.findViewById(R.id.nagravisionprojects);
final View v2 = tablayout.findViewById(R.id.cryptoworksprojects);
mTabHost.setup();
mTabHost.addTab(mTabHost.newTabSpec(&nagravision&).setIndicator(&Nagravision&)
.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v1;
mTabHost.addTab(mTabHost.newTabSpec(&cryptoworks&).setIndicator(&Cryptoworks&)
.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v2;
mTabHost.setCurrentTab(0);
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(mCurrentTab);
public static class Nagravision_Fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.moodnagravision, container, false);
public static class Cryptoworks_Fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.moodcryptoworks, container, false);
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
FragmentTransaction FrgTra = getFragmentManager().beginTransaction();
if (tabId.equals(&nagravision&)) {
if (getFragmentManager().findFragmentByTag(tabId) == null) {
FrgTra.replace(R.id.nagravisionprojects, new Nagravision_Fragment(),
tabId).addToBackStack(null).commit();
if (tabId.equals(&cryptoworks&)) {
if (getFragmentManager().findFragmentByTag(tabId) == null) {
FrgTra.replace(R.id.cryptoworksprojects, new
Cryptoworks_Fragment(), tabId).addToBackStack(null).commit();
public void onItemClick(AdapterView&?& parent, View view, int position, long id) {
// TODO Auto-generated method stub
public View createTabContent(String tag) {
// TODO Auto-generated method stub
其中projects布局文件为
&?xml version=&1.0& encoding=&utf-8&?&
&TabHost xmlns:android=&/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:layout_weight=&0& /&
&FrameLayout
android:id=&@android :id/tabcontent&
android:layout_width=&0dp&
android:layout_height=&0dp&
android:layout_weight=&0& &
&FrameLayout
android:id=&@+id/nagravisionprojects&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&/FrameLayout&
&FrameLayout
android:id=&@+id/cryptoworksprojects&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&/FrameLayout&
&/FrameLayout&
&/LinearLayout&
&/TabHost&
moodnagravision和moodcryptoworks布局类似为
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:id=&@+id/moodnagravision1&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&ImageView
android:id=&@+id/moodnagravision2&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:src=&@drawable/ic_sync_green& /&
android:id=&@+id/moodnagravision3&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&@string/nagtavision&
android:textSize=&20.0dip& /&
&/RelativeLayout&
共有8个答案
<span class="a_vote_num" id="a_vote_num_
求救啊。。怎么解决的
--- 共有 1 条评论 ---
我的是TabHost布局文件的问题,尤其是tab_widget中的,我现在改成&include layout="@layout/tab_widget" /&
其中tab_widget布局文件中为HorizontalScrollView,你可以参考应用管理里边的。
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
把你的Activity贴出来看看
--- 共有 1 条评论 ---
上边就是类呀,我是想再settings下做个类似应该管理的界面。
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
没懂你意思,什么叫内容不显示
--- 共有 1 条评论 ---
就是moodnagravision和moodcryptoworks的布局不能显示啊!
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
你好像少了& tablayout.setTag();你只用tablayout& 把你要的布局加进来了,没有把内容set 进来。
--- 共有 2 条评论 ---
把你 加载内容的 set进来。应该是这样的。目前就看出来这一点,你试下吧
(2年前)&nbsp&
该在什么地方加了?
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
holder.setTime((TextView) convertView.findViewById(R.id.tv_time));
holder.setContent((TextView) convertView.findViewById(R.id.tv_content));
holder.setUserImage((ImageView) convertView.findViewById(R.id.iv_user_image));
convertView.setTag(holder);
holder = (ChartHolder) convertView.getTag();
holder.getTime().setText(chart.get(position).getTime());
holder.getContent().setText(chart.get(position).getContent());
holder.getUserImage().setImageResource(chart.get(position).getUserImage());
return convertV convertView 是个 View& holder 是加载要显示的内容的。要
convertView.setTag(holder); 才能将 加载的内容& 加入到控件
--- 共有 4 条评论 ---
moodnagravision.xml是布局文件,要显示的内容!
(2年前)&nbsp&
: 你这什么? moodnagravision是布局定义的ID?
(2年前)&nbsp&
有个问题,文本和图片控件在moodnagravision和moodcryptoworks布局文件中,我改如何去加载数据了?
(2年前)&nbsp&
我理解你的意思了,我试试。
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
哈哈,已经解决。。。。
--- 共有 3 条评论 ---
: 看不到你的代码。不知道你的问题何在!
(1年前)&nbsp&
求解决办法
(1年前)&nbsp&
请问你是怎么解决的?我遇到了同样的问题,不知道怎么办
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
Did you forget to call 'public void setup(LocalActivityManager activityGroup
--- 共有 1 条评论 ---
你的类应该继承ActivityGroup类吧,才能获得ActivityGroup
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
你知道怎么解决么?
有什么技术问题吗?
chengch...的其他问题
类似的话题FragmentTabHost中的tab对应的Fragment不被OnDestoryView-android100学习网
FragmentTabHost中的tab对应的Fragment不被OnDestoryView
问题:从上图的这个界面跳转到某个联系人的具体界面,但是上图中的fragment没有执行onPause(),onStop(),onDestoryView()方法,于是这个为被销毁的界面会影响
从上图的这个界面跳转到某个联系人的具体界面,但是上图中的fragment没有执行onPause(),onStop(),onDestoryView()方法,于是这个为被销毁的界面会影响联系人具体界面的操作。
所以不得不,手动摧毁这个界面,于是,在载入联系人具体界面之前,现将上图界面hide()
fragment = fm.findFragmentByTag("0").getChildFragmentManager().findFragmentByTag("contacts");
if(fragment!=null){
Log.e(TAG, "contacts已存在");
fragment.onPause();
fragment.onStop();
fragment.onDestroyView();
fm.beginTransaction().hide(fragment).commit();
此时,如果在联系人具体界面点击返回按钮,上面中的界面会为空,是因为,界面没有被show()出来,所以监听返回按钮,show()这个界面。
// 监听实体回退按钮,只有对view设置了焦点,回退按钮 和 向上导航才能被监听到
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
fragment = (ContactsFragmentTab) fm.findFragmentByTag("0").getChildFragmentManager().findFragmentByTag("contacts");
fm.beginTransaction().show(fragment).commit();
});监听向上导航
public boolean onOptionsItemSelected(MenuItem item) {
Log.e(TAG, "onOptionsItemSelected");
switch (item.getItemId()) {
case android.R.id.home:
fragment = (ContactsFragmentTab) fm.findFragmentByTag("0").getChildFragmentManager().findFragmentByTag("contacts");
fm.beginTransaction().show(fragment).commit();
// called when the up affordance/carat in actionbar is pressed
getActivity().onBackPressed();
return super.onOptionsItemSelected(item);
注:为了fragment的重用,fragment与其他fragment的交互最好都放在他们的activity中进行,使用接口来实现之间的相互操作,见另外几篇博文:
http://blog.csdn.net/zqx198810/article/details/9401475后使用快捷导航没有帐号?
&&活动专区
&&高德地图专区
&&高德导航专区
&&站务管理
只需一步,快速开始
解决 mapview 在tabhost viewpager fragment中闪屏的问题
潜力级地图控
本帖最后由 witype716 于
22:44 编辑
& && & 笔者最近一段时间开发地图需要选择了高德地图做产品的地图控件
我们的产品的主界面起初采用的是一个tabhost但是tabhost在切换的时候总是要闪屏通过我的研究找到了以下解决办
& && &原因解析:因为tabhost或者viewpager加载视图的时候因为地图本身很占用系统资源同时加载的话就会导致闪屏.
& && &分析:mapview在全局是单例的也就是说无论你在什么地方或者有多少个viewpager&&tabhost 那么他都是同一个对象我们为什么在要同时初始化地图视图和其他视图因为加载地图闪屏都是出现在 地图所在的视图不是tabhost或者viewpager第一个界面(第一次添加)
& & 解决办法:我们可以在程序的欢迎界面初始化地图
& && && &mapView = (MapView)findViewById(R.id.map);
& && &&&mapView.onCreate(savedInstanceState);
& &&&* 初始化AMap对象
& & private void init() {
& && &&&if (aMap == null) {
& && && && &aMap = mapView.getMap();
这样的话地图控制在进入主界面的时候已经加载好了一次在加载的话就是直接应用.就不会出现这种情况.
当然这还要待大家的共同验证也请开发者们debug
城市级地图控
感谢您写的帖子
这个帖子啥意思,是说怎么用?求解释&
潜力级地图控
把mapview add到listviewhead ,还是有闪黑的情况出现
乡镇级地图控
感谢您写的帖子
这个帖子啥意思,是说怎么用?求解释
潜力级地图控
真他们的傻逼,你在欢迎界面能找到 子view的 mapview??????使用FragmentTabhost代替Tabhost
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 fragment嵌套tabhost 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信