FramentTabHost可以在底部增加类似微信底部的按钮。但是现在有一个需求,点击其中一个按钮,需要显示4个圆形的按钮,并且背景是透明的。这样需要一个style是Dialog的Activity。如何才能做到呢,因为普通FragmentTabHost只能添加Fragment。
我们先看普通的FragmentTabHost是如何添加一个tab的。
mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FragmentStackSupport.CountingFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), LoaderCursorSupport.CursorLoaderListFragment.class, null);再来看看FragmentTabHost中的addTab方法
public class FragmentTabHost extends TabHost { public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { ... addTab(tabSpec); } }再跟下Tabhost的addTab代码
public class TabHost extends FrameLayout { private TabWidget mTabWidget; public void addTab(TabSpec tabSpec) { ... View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView(); ... mTabWidget.addView(tabIndicator); ... } }
addTab方法中调用了TabWidget的addView方法
public class TabWidget extends LinearLayout { @Override public void addView(View child) { ... super.addView(child); // TODO: detect this via geometry with a tabwidget listener rather // than potentially interfere with the view's listener child.setOnClickListener(new TabClickListener(getTabCount() - 1)); child.setOnFocusChangeListener(this); } }在addView 方法中调用了父类的addView方法,最后调用setOnClickListener方法。
看到这里我们知道可以自己去设置setOnclickListener方法,
mTabHost.getTabWidget().getChildTabViewAt(0).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
下面我么再看下我们的代码
public class DetailActivity extends FragmentActivity { private String TAG = "DetailActivity"; private FragmentTabHost mTabHost; private LayoutInflater layoutInflater; private String mMallId; private String mMallName; private String mSearchKey; private Class fragmentArray[] = { FragmentHome.class, FragmentShow.class, FragmentDown.class, FragmentUser.class, FragmentSetting.class }; private int mImageViewArray[] = { R.drawable.home, R.drawable.show, R.drawable.discover_btn, R.drawable.map, R.drawable.user }; private String mTextviewArray[] = { "主页", "秀吧", "发现", "用户", "设置" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); Intent intent = getIntent(); mMallId = intent.getStringExtra("mallId"); mMallName = intent.getStringExtra("mallName"); mSearchKey = intent.getStringExtra("searchKey"); initView(); } private void initView() { layoutInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); int count = fragmentArray.length; for (int i = 0; i < count; i++) { TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]) .setIndicator(getTabItemView(i)); mTabHost.addTab(tabSpec, fragmentArray[i], null); /*mTabHost.getTabWidget().getChildAt(i) .setBackgroundResource(R.drawable.showbar_backgroud);*/ } mTabHost.getTabWidget().setBackgroundResource(R.drawable.showbar_backgroud); mTabHost.getTabWidget().getChildTabViewAt(2).setOnClickListener(new View.OnClickListener() {//第3个按钮,点击事件我们这边处理 @Override public void onClick(View v) { Intent intent = new Intent(DetailActivity.this, DiscoverWindow.class); startActivity(intent); } }); } private View getTabItemView(int index) { View view = layoutInflater.inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview); if (index == 2) { Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.discover_btn); Bitmap newBitmap = BitmapZoom.bitmapZoomByWidth(bitmap, getScreenWidth() / 8); imageView.setImageBitmap(newBitmap); } else { imageView.setImageResource(mImageViewArray[index]); } return view; }上面底部第三个按钮我们用自己的click事件处理,也就是不会到原生的Fragment中去了,而在我们的click事件处理中,我们startActivity。因此就实现了在FragmentTabHost中增加了一个Activity,而不是去进入另一个Fragment。这样前面一个Fragment就不会销毁,进入另一个Fragment。进入另一个Acitivity的话,可以把style变成Dialog,背景变成透明的可以实现了。