/** * Creates the widget that holds the actual tabs. * * @param activity Activity to create the widgets in. * @return the widget that holds the actual tabs. */ private TabWidget createTabWidget(Activity activity) { TabWidget tabWidget = new TabWidget(activity); tabWidget.setId(android.R.id.tabs); LinearLayout.LayoutParams tabWidgetParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); tabWidgetParams.weight = 0; tabWidget.setLayoutParams(tabWidgetParams); return tabWidget; }
/** * 获取TabHost * * @param mContext * @param gravity tabs的位置,上面TOP 下面BOTTOM,目前只支持两个 * @return */ public static EasyFragmentTabHostSaveState getKFragmentTabHostSaveStateView( Context mContext, int gravity) { // init FragmentTabHost EasyFragmentTabHostSaveState tabhost = new EasyFragmentTabHostSaveState(mContext); tabhost.setId(android.R.id.tabhost); tabhost.setLayoutParams( new FragmentTabHost.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); LinearLayout mLinearLayout = new LinearLayout(mContext); mLinearLayout.setOrientation(LinearLayout.VERTICAL); mLinearLayout.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // init FrameLayout FrameLayout realtabcontent = new FrameLayout(mContext); realtabcontent.setId(EasyR.id.realtabcontent); realtabcontent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)); // init TabWidget TabWidget tabs = new TabWidget(mContext); tabs.setId(android.R.id.tabs); tabs.setLayoutParams( new TabWidget.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0)); tabs.setOrientation(LinearLayout.HORIZONTAL); FrameLayout tabcontent = new FrameLayout(mContext); tabcontent.setId(android.R.id.tabcontent); tabcontent.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f)); tabhost.addView(mLinearLayout); switch (gravity) { case TabGravity.BOTTOM: mLinearLayout.addView(realtabcontent); mLinearLayout.addView(tabs); // wiget在下面 mLinearLayout.addView(tabcontent); break; case TabGravity.TOP: mLinearLayout.addView(tabs); // wiget在上面 mLinearLayout.addView(tabcontent); mLinearLayout.addView(realtabcontent); break; } return tabhost; }
/** * 获取TabHost * * @param mContext * @param gravity tabs的位置,上面TOP 下面BOTTOM,目前只支持两个 * @return */ public static TabHost getTabHostAndPagerView(Context mContext, int tabGravity) { // init TabHost TabHost tabhost = new TabHost(mContext); tabhost.setId(android.R.id.tabhost); tabhost.setLayoutParams( new FragmentTabHost.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); LinearLayout mLinearLayout = new LinearLayout(mContext); mLinearLayout.setOrientation(LinearLayout.VERTICAL); mLinearLayout.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // init TabWidget TabWidget tabs = new TabWidget(mContext); tabs.setId(android.R.id.tabs); tabs.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0)); tabs.setOrientation(LinearLayout.HORIZONTAL); FrameLayout tabcontent = new FrameLayout(mContext); tabcontent.setId(android.R.id.tabcontent); tabcontent.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f)); ViewPager viewPager = new ViewPager(mContext); viewPager.setId(EasyR.id.pager); viewPager.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)); tabhost.addView(mLinearLayout); switch (tabGravity) { case TabGravity.BOTTOM: mLinearLayout.addView(viewPager); mLinearLayout.addView(tabs); // wiget在下面 mLinearLayout.addView(tabcontent); break; case TabGravity.TOP: default: mLinearLayout.addView(tabs); // wiget在上面 mLinearLayout.addView(tabcontent); mLinearLayout.addView(viewPager); break; } return tabhost; }
public MaterialTabHost(Context context, AttributeSet attrs) { super(context, attrs); inflater = LayoutInflater.from(context); TypedValue outValue = new TypedValue(); Resources.Theme theme = context.getTheme(); // use ?attr/colorPrimary as background color // theme.resolveAttribute(R.attr.colorPrimary, outValue, true); // setBackgroundColor(outValue.data); // use ?attr/colorControlActivated as default indicator color // theme.resolveAttribute(R.attr.colorControlActivated, outValue, true); // colorControlActivated = outValue.data; setBackgroundColor(0xff0CACF4); colorControlActivated = 0xffBEEAFC; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaterialTabHost, 0, 0); int indicatorColor = a.getColor(R.styleable.MaterialTabHost_colorTabIndicator, colorControlActivated); a.recycle(); // ColorDrawable on 2.x does not use getBounds() so use ShapeDrawable indicator = new ShapeDrawable(); indicator.setColorFilter(indicatorColor, PorterDuff.Mode.SRC_ATOP); Resources res = context.getResources(); indicatorHeight = res.getDimensionPixelSize(R.dimen.mth_tab_indicator_height); leftOffset = res.getDimensionPixelSize(R.dimen.mth_tab_left_offset); // int tabHeight = res.getDimensionPixelSize(R.dimen.mth_tab_height); tabWidget = new TabWidget(context); tabWidget.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); tabWidget.setId(android.R.id.tabs); tabWidget.setStripEnabled(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { tabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); } addView(tabWidget); FrameLayout fl = new FrameLayout(context); fl.setLayoutParams(new LayoutParams(0, 0)); fl.setId(android.R.id.tabcontent); addView(fl); setup(); setOnTabChangedListener( new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { if (listener != null) { listener.onTabSelected(Integer.valueOf(tabId)); } } }); float density = getResources().getDisplayMetrics().density; // set elevation for App bar // http://www.google.com/design/spec/what-is-material/objects-in-3d-space.html#objects-in-3d-space-elevation ViewCompat.setElevation(this, APP_TAB_ELEVATION * density); }