大半夜了,这个问题终于解决了,相信很多人都用过FragmentTabHost写过类似于微信啥的那种,如果的你的v4兼容包是在版本18以前的,你的布局写成这样
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
"@+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
"@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFF1F1F1"/>
"@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
.support.v4.app.FragmentTabHost>
不会有任何问题,但是如果你的v4包版本在18以上(这里我指的不包括18),就会报一个No tab content FrameLayout found for id xxx的错误,这个异常在v4包的FragmentTabHost中,是这个部分:
private void ensureContent() {
if (mRealTabContent == null) {
mRealTabContent = (FrameLayout)findViewById(mContainerId);
if (mRealTabContent == null) {
throw new IllegalStateException(
"No tab content FrameLayout found for id " + mContainerId);
}
}
}
但是新旧版的代码都有这个方法,Google了一下基本都说替换v4包,我说tm这尼玛也叫解决方法,果断stackoverflow泡了一会,无果,又撸了一会FragmentTabHost的代码,貌似区别在这里
private void initFragmentTabHost(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
new int[] { android.R.attr.inflatedId }, 0, 0);
mContainerId = a.getResourceId(0, 0);
a.recycle();
super.setOnTabChangedListener(this);
}
private void ensureHierarchy(Context context) {
// If owner hasn't made its own view hierarchy, then as a convenience
// we will construct a standard one here.
if (findViewById(android.R.id.tabs) == null) {
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
addView(ll, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
TabWidget tw = new TabWidget(context);
tw.setId(android.R.id.tabs);
tw.setOrientation(TabWidget.HORIZONTAL);
ll.addView(tw, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0));
private void ensureHierarchy(Context context) {在原有代码上抽出来一个方法,但是并没想到什么头绪,我还真用了替换v4包的方法,换到了旧的18版本,结果到了今天添加一个github码的时候发现gg了,人家用的版本22,这个棘手的问题又得面对了;Google的时候无意间看见了一个说FragmentTabHost的bug,但是还不是很明白、大意就是顺序的问题,结果机智了一下、又stackoverflow,还真找到了,
stackoverflow地址问题在布局上面!上码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/activity_main_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
LinearLayout>
android.support.v4.app.FragmentTabHost>
我的Java码是这样写的:
package com.wxh.chattest;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost;
import com.wxh.chattest.base.BaseActivity;
import com.wxh.chattest.fragment.MemberFragment;
import com.wxh.chattest.fragment.MyFragment;
import com.wxh.chattest.widget.TabIndicatorView;
public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener {
private static final String TAG_MAIN = "main";
private static final String TAG_FRIEND = "friend";
private static final String TAG_LOG = "log";
private static final String TAG_ME = "me";
private FragmentTabHost tabHost;
private TabIndicatorView MainIndicators;
private TabIndicatorView FriendIndicators;
private TabIndicatorView LogIndicators;
private TabIndicatorView MeIndicators;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
//初始化tabhost
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.activity_main_container);
//添加tabspec
TabHost.TabSpec spec = tabHost.newTabSpec(TAG_MAIN);
MainIndicators = new TabIndicatorView(this);
MainIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus);
MainIndicators.setTabTitle("主页");
spec.setIndicator(MainIndicators);
//添加tab
tabHost.addTab(spec, MyFragment.class, null);
//添加tabspec
spec = tabHost.newTabSpec(TAG_FRIEND);
FriendIndicators = new TabIndicatorView(this);
FriendIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus);
FriendIndicators.setTabTitle("朋友");
spec.setIndicator(FriendIndicators);
//添加tab
tabHost.addTab(spec, MyFragment.class, null);
//添加tabspec
spec = tabHost.newTabSpec(TAG_LOG);
LogIndicators = new TabIndicatorView(this);
LogIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus);
LogIndicators.setTabTitle("日志");
spec.setIndicator(LogIndicators);
//添加tab
tabHost.addTab(spec, MyFragment.class, null);
//添加tabspec
spec = tabHost.newTabSpec(TAG_ME);
MeIndicators = new TabIndicatorView(this);
MeIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus);
MeIndicators.setTabTitle("我的");
spec.setIndicator(MeIndicators);
//添加tab
tabHost.addTab(spec,MemberFragment.class, null);
//去白线
tabHost.getTabWidget().setDividerDrawable(android.R.color.white);
//监听
tabHost.setCurrentTabByTag(TAG_MAIN);
MainIndicators.setTabSelected(true);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tag) {
MainIndicators.setTabSelected(false);
FriendIndicators.setTabSelected(false);
LogIndicators.setTabSelected(false);
MeIndicators.setTabSelected(false);
if (TAG_MAIN.equals(tag)) {
MainIndicators.setTabSelected(true);
} else if (TAG_FRIEND.equals(tag)) {
FriendIndicators.setTabSelected(true);
} else if (TAG_LOG.equals(tag)) {
LogIndicators.setTabSelected(true);
} else if (TAG_ME.equals(tag)) {
MeIndicators.setTabSelected(true);
}
}
}
TabIndicatorView是我自定义的一个组合式控件