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.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); TabWidget tw = new TabWidget(context); tw.setId(android.R.id.tabs); tw.setOrientation(TabWidget.HORIZONTAL); ll.addView( tw, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); FrameLayout fl = new FrameLayout(context); fl.setId(android.R.id.tabcontent); ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0)); mRealTabContent = fl = new FrameLayout(context); mRealTabContent.setId(mContainerId); ll.addView(fl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1)); } }
/** * 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; }
private void initFragmentTabHost(Context paramContext, AttributeSet paramAttributeSet) { TypedArray localTypedArray = paramContext.obtainStyledAttributes(paramAttributeSet, new int[] { 16842995 }, 0, 0); this.mContainerId = localTypedArray.getResourceId(0, 0); localTypedArray.recycle(); super.setOnTabChangedListener(this); if (findViewById(16908307) != null) return; LinearLayout localLinearLayout = new LinearLayout(paramContext); localLinearLayout.setOrientation(1); addView(localLinearLayout, new FrameLayout.LayoutParams(-1, -1)); TabWidget localTabWidget = new TabWidget(paramContext); localTabWidget.setId(16908307); localTabWidget.setOrientation(0); localLinearLayout.addView(localTabWidget, new LinearLayout.LayoutParams(-1, -2, 0.0F)); FrameLayout localFrameLayout1 = new FrameLayout(paramContext); localFrameLayout1.setId(16908305); localLinearLayout.addView(localFrameLayout1, new LinearLayout.LayoutParams(0, 0, 0.0F)); FrameLayout localFrameLayout2 = new FrameLayout(paramContext); this.mRealTabContent = localFrameLayout2; this.mRealTabContent.setId(this.mContainerId); localLinearLayout.addView(localFrameLayout2, new LinearLayout.LayoutParams(-1, 0, 1.0F)); }
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); }
@SuppressWarnings("unchecked") public TabbedMainView(Object params) { Context ctx = RhodesActivity.getContext(); mBackgroundColorEnable = false; Vector<Object> tabs = null; boolean place_tabs_bottom = false; if (params instanceof Vector<?>) tabs = (Vector<Object>) params; else if (params instanceof Map<?, ?>) { Map<Object, Object> settings = (Map<Object, Object>) params; Object bkgObj = settings.get("background_color"); if ((bkgObj != null) && (bkgObj instanceof String)) { int color = Integer.parseInt((String) bkgObj) | 0xFF000000; mBackgroundColor = color; mBackgroundColorEnable = true; } Object callbackObj = settings.get("on_change_tab_callback"); if ((callbackObj != null) && (callbackObj instanceof String)) { mChangeTabCallback = new String(((String) callbackObj)); } Object placeBottomObj = settings.get("place_tabs_bottom"); if ((placeBottomObj != null) && (placeBottomObj instanceof String)) { place_tabs_bottom = ((String) placeBottomObj).equalsIgnoreCase("true"); } Object tabsObj = settings.get("tabs"); if (tabsObj != null && (tabsObj instanceof Vector<?>)) tabs = (Vector<Object>) tabsObj; } if (tabs == null) throw new IllegalArgumentException("No tabs specified"); int size = tabs.size(); host = new TabHost(ctx, null); tabData = new Vector<TabData>(size); tabIndex = 0; TabWidget tabWidget = new TabWidget(ctx); tabWidget.setId(android.R.id.tabs); FrameLayout frame = new FrameLayout(ctx); FrameLayout.LayoutParams lpf = null; TabHost.LayoutParams lpt = null; if (place_tabs_bottom) { frame.setId(android.R.id.tabcontent); lpf = new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP); host.addView(frame, lpf); lpt = new TabHost.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); host.addView(tabWidget, lpt); } else { lpt = new TabHost.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.TOP); host.addView(tabWidget, lpt); frame = new FrameLayout(ctx); frame.setId(android.R.id.tabcontent); lpf = new FrameLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.BOTTOM); host.addView(frame, lpf); } host.setup(); TabHost.TabSpec spec; DisplayMetrics metrics = new DisplayMetrics(); WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metrics); int selected_color = 0; boolean selected_color_enable = false; for (int i = 0; i < size; ++i) { Object param = tabs.elementAt(i); if (!(param instanceof Map<?, ?>)) throw new IllegalArgumentException("Hash expected"); Map<Object, Object> hash = (Map<Object, Object>) param; Object labelObj = hash.get("label"); if (labelObj == null || !(labelObj instanceof String)) throw new IllegalArgumentException("'label' should be String"); Object actionObj = hash.get("action"); boolean use_current_view_for_tab = false; Object use_current_view_for_tab_Obj = hash.get("use_current_view_for_tab"); if (use_current_view_for_tab_Obj != null) { use_current_view_for_tab = ((String) use_current_view_for_tab_Obj).equalsIgnoreCase("true"); } if (use_current_view_for_tab) { actionObj = new String("none"); } if (actionObj == null || !(actionObj instanceof String)) throw new IllegalArgumentException("'action' should be String"); String label = (String) labelObj; String action = (String) actionObj; String icon = null; boolean reload = false; boolean disabled = false; int web_bkg_color = 0xFFFFFFFF; Object iconObj = hash.get("icon"); if (iconObj != null && (iconObj instanceof String)) icon = "apps/" + (String) iconObj; Object reloadObj = hash.get("reload"); if (reloadObj != null && (reloadObj instanceof String)) reload = ((String) reloadObj).equalsIgnoreCase("true"); Object selected_color_Obj = hash.get("selected_color"); if ((selected_color_Obj != null) && (selected_color_Obj instanceof String)) { selected_color_enable = true; selected_color = Integer.parseInt((String) selected_color_Obj) | 0xFF000000; } Object disabled_Obj = hash.get("disabled"); if (disabled_Obj != null && (disabled_Obj instanceof String)) disabled = ((String) disabled_Obj).equalsIgnoreCase("true"); Object web_bkg_color_Obj = hash.get("web_bkg_color"); if (web_bkg_color_Obj != null && (web_bkg_color_Obj instanceof String)) { web_bkg_color = Integer.parseInt((String) web_bkg_color_Obj) | 0xFF000000; } spec = host.newTabSpec(Integer.toString(i)); // Set label and icon BitmapDrawable drawable = null; if (icon != null) { String iconPath = RhoFileApi.normalizePath(icon); Bitmap bitmap = BitmapFactory.decodeStream(RhoFileApi.open(iconPath)); if (disabled && (bitmap != null)) { // replace Bitmap to gray bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); // prepare mutable bitmap int x; int y; int bw = bitmap.getWidth(); int bh = bitmap.getHeight(); int nc = DISABLED_IMG_COLOR & 0xFFFFFF; int c; for (y = 0; y < bh; y++) { for (x = 0; x < bw; x++) { c = bitmap.getPixel(x, y); c = nc | (c & 0xFF000000); bitmap.setPixel(x, y, c); } } } if (bitmap != null) bitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM); // Bitmap.DENSITY_NONE); drawable = new BitmapDrawable(bitmap); drawable.setTargetDensity(metrics); } if (drawable == null) spec.setIndicator(label); else spec.setIndicator(label, drawable); SimpleMainView view = null; if (use_current_view_for_tab) { RhodesService r = RhodesService.getInstance(); MainView mainView = r.getMainView(); action = mainView.currentLocation(-1); view = new SimpleMainView(mainView); } else { view = new SimpleMainView(); } // Set view factory if (web_bkg_color_Obj != null) { if (!use_current_view_for_tab) { view.setWebBackgroundColor(web_bkg_color); } host.setBackgroundColor(web_bkg_color); } TabData data = new TabData(); data.view = view; data.url = action; data.reload = reload; if (use_current_view_for_tab) { data.loaded = true; tabIndex = i; } data.selected_color = selected_color; data.selected_color_enabled = selected_color_enable; data.disabled = disabled; TabViewFactory factory = new TabViewFactory(data); spec.setContent(factory); tabData.addElement(data); host.addTab(spec); } tabWidget.measure(host.getWidth(), host.getHeight()); int hh = tabWidget.getMeasuredHeight(); // if (hh < 64) { // hh = 64; // } if (place_tabs_bottom) { lpf.setMargins(0, 0, 0, hh); } else { lpf.setMargins(0, hh, 0, 0); } host.updateViewLayout(frame, lpf); }