/** @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int) */ public static void setMarginEnd(MarginLayoutParams layoutParams, int end) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { layoutParams.setMarginEnd(end); } else { layoutParams.rightMargin = end; } }
/** @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int) */ public static void setMarginStart(MarginLayoutParams layoutParams, int start) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { layoutParams.setMarginStart(start); } else { layoutParams.leftMargin = start; } }
private void addTableRow(String key, String val) { TableLayout tl = (TableLayout) findViewById(R.id.data_table); TableRow tr = new TableRow(this); MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN); tr.setLayoutParams(params); tr.setBackgroundColor(Color.BLACK); TextView name = new TextView(this); name.setGravity(Gravity.RIGHT); name.setText(key + ": "); TextView value = new TextView(this); value.setGravity(Gravity.LEFT); value.setText(val); tr.addView(name); tr.addView(value); tl.addView( tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); /* * TODO remove this hack * * let's define a limit number of rows */ if (tl.getChildCount() > 10) tl.removeViewAt(0); }
private QueryItem createItem() { QueryItem item = new QueryItem(getActivity()); MarginLayoutParams params = new MarginLayoutParams(Constant.WRAP_CONTENT, Constant.WRAP_CONTENT); params.topMargin = 20; item.setLayoutParams(params); return item; }
public void restoreMarginLayoutParams(MarginLayoutParams params) { this.restoreLayoutParams(params); params.leftMargin = this.mPreservedParams.leftMargin; params.topMargin = this.mPreservedParams.topMargin; params.rightMargin = this.mPreservedParams.rightMargin; params.bottomMargin = this.mPreservedParams.bottomMargin; MarginLayoutParamsCompat.setMarginStart( params, MarginLayoutParamsCompat.getMarginStart(this.mPreservedParams)); MarginLayoutParamsCompat.setMarginEnd( params, MarginLayoutParamsCompat.getMarginEnd(this.mPreservedParams)); }
/** * 设置View的底部外边距(像素值) * * @param view * @return */ public static boolean setMarginBottom(View view, int marginBottom) { MarginLayoutParams marginParams = null; try { marginParams = (MarginLayoutParams) view.getLayoutParams(); } catch (ClassCastException e) { return false; } if (marginParams == null) return false; marginParams.bottomMargin = marginBottom; view.setLayoutParams(marginParams); return true; }
public void show(int touchX, int touchY) { this.mL = touchX - this.mRegistrationX; this.mT = touchY - this.mRegistrationY; this.mR = (touchX - this.mRegistrationX) + this.mWidth; this.mB = (touchY - this.mRegistrationY) + this.mHeight; this.mLayout.addView(this); MarginLayoutParams mp = (MarginLayoutParams) getLayoutParams(); mp.leftMargin = this.mL; mp.topMargin = this.mT; mp.rightMargin = this.mR; mp.bottomMargin = this.mB; setLayoutParams(mp); }
/** Initialize necessary items such as database(helper), lists etc. */ @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.home_layout, container, false); // Retrieve databasehelper recipesHelper = ((MainActivity) this.getActivity()).getDatabaseHelper(); // Initialize textview with introduction TextView tv = (TextView) v.findViewById(R.id.text); tv.setText( "Welcome to the Hotmeals App!" + '\n' + "You can search for recipes by pressing the search tab," + '\n' + "or view recipes that you've marked as a favorite by pressing the favorites tab!" + '\n' + "Or if you like, browse the recipes you've recently viewed down below."); // Set top margins so that no overlap occurs MarginLayoutParams margins = (MarginLayoutParams) tv.getLayoutParams(); margins.topMargin = ((MainActivity) this.getActivity()).getTabBarHeight(); tv.setLayoutParams(margins); recipesReadableDatabase = recipesHelper.getReadableDatabase(); // Init database list Cursor cursor = getRecentRecipes(); recentRecipes = getRecipesFromCursor(cursor); cursor.close(); // Done with database recipesReadableDatabase.close(); // Populate list final ListView recentsList = (ListView) v.findViewById(R.id.recentsList); RecipeListViewAdapter adapter = new RecipeListViewAdapter(getActivity(), recipeArrayListToArray(recentRecipes)); recentsList.setAdapter(adapter); // When recipe in list clicked, go to recipe recentsList.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Recipe recipe = (Recipe) recentsList.getItemAtPosition(position); goToRecipe(recipe); } }); return v; }
/** * 设置View的外边距(像素值) * * @param view * @param left * @param top * @param right * @param bottom * @return */ public static boolean setMargin(View view, int left, int top, int right, int bottom) { MarginLayoutParams marginParams = null; try { marginParams = (MarginLayoutParams) view.getLayoutParams(); } catch (ClassCastException e) { return false; } if (marginParams == null) return false; marginParams.leftMargin = left; marginParams.topMargin = top; marginParams.rightMargin = right; marginParams.bottomMargin = bottom; view.setLayoutParams(marginParams); return true; }
/** * 重新计算view的Margin * * @param view * @return */ public static void remargin(View view, float horizontalRatio, float verticalRatio) { MarginLayoutParams marginParams = null; try { marginParams = (MarginLayoutParams) view.getLayoutParams(); } catch (ClassCastException e) { return; } if (marginParams == null) return; int left = (int) (marginParams.leftMargin * horizontalRatio); int top = (int) (marginParams.topMargin * verticalRatio); int right = (int) (marginParams.rightMargin * horizontalRatio); int bottom = (int) (marginParams.bottomMargin * verticalRatio); marginParams.setMargins(left, top, right, bottom); view.setLayoutParams(marginParams); }
private void loadVPlayerPrefs() { if (!isInitialized()) return; vPlayer.setBuffer(VP.DEFAULT_BUF_SIZE); vPlayer.setVideoQuality(VP.DEFAULT_VIDEO_QUALITY); vPlayer.setDeinterlace(VP.DEFAULT_DEINTERLACE); vPlayer.setVolume(VP.DEFAULT_STEREO_VOLUME, VP.DEFAULT_STEREO_VOLUME); vPlayer.setSubEncoding(VP.DEFAULT_SUB_ENCODING); MarginLayoutParams lp = (MarginLayoutParams) mSubtitleContainer.getLayoutParams(); lp.bottomMargin = (int) VP.DEFAULT_SUB_POS; mSubtitleContainer.setLayoutParams(lp); vPlayer.setSubShown(mSubShown); setTextViewStyle(mSubtitleText); if (!TextUtils.isEmpty(mSubPath)) vPlayer.setSubPath(mSubPath); if (mVideoView != null && isInitialized()) setVideoLayout(); }
/** @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd() */ public static int getMarginEnd(MarginLayoutParams layoutParams) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { return layoutParams.getMarginEnd(); } else { return layoutParams.rightMargin; } }
/* * Sets contact photo to a default placeholder image */ private void setContactPhotoToDefault(ImageView photoImageView) { // Reset background and padding photoImageView.setBackgroundResource(0); photoImageView.setPadding(0, 0, 0, 0); // Set margins for placeholder image MarginLayoutParams mLP = (MarginLayoutParams) photoImageView.getLayoutParams(); final int scaledMargin = (int) (contactPhotoDefaultMargin * this.getResources().getDisplayMetrics().density); mLP.setMargins(scaledMargin, scaledMargin, scaledMargin, scaledMargin); photoImageView.setLayoutParams(mLP); // Set placeholder image photoImageView.setImageDrawable(contactPhotoPlaceholderDrawable); }
private static void resetViewLayoutParams(View view, float scale) { if (view instanceof TextView) { resetTextSize((TextView) view, scale); } int pLeft = convertFloatToInt(view.getPaddingLeft() * scale); int pTop = convertFloatToInt(view.getPaddingTop() * scale); int pRight = convertFloatToInt(view.getPaddingRight() * scale); int pBottom = convertFloatToInt(view.getPaddingBottom() * scale); view.setPadding(pLeft, pTop, pRight, pBottom); LayoutParams params = view.getLayoutParams(); if ("test_relayout".equals(view.getTag())) { Log.i("relayout", "test_relayout/" + params); } if (params == null) { return; } if (params.width > 0) { params.width = convertFloatToInt(params.width * scale); } if (params.height > 0) { params.height = convertFloatToInt(params.height * scale); } if (params instanceof MarginLayoutParams) { MarginLayoutParams mParams = (MarginLayoutParams) params; if (mParams.leftMargin > 0) { mParams.leftMargin = convertFloatToInt(mParams.leftMargin * scale); } if (mParams.rightMargin > 0) { mParams.rightMargin = convertFloatToInt(mParams.rightMargin * scale); } if (mParams.topMargin > 0) { mParams.topMargin = convertFloatToInt(mParams.topMargin * scale); } if (mParams.bottomMargin > 0) { mParams.bottomMargin = convertFloatToInt(mParams.bottomMargin * scale); } } }
private void addTableRow(String id, String key, String val) { TableRow tr = new TableRow(this); MarginLayoutParams params = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN); tr.setLayoutParams(params); TextView name = new TextView(this); name.setGravity(Gravity.RIGHT); name.setText(key + ": "); TextView value = new TextView(this); value.setGravity(Gravity.LEFT); value.setText(val); value.setTag(id); tr.addView(name); tr.addView(value); tl.addView(tr, params); }
public void fillMarginLayoutParams(MarginLayoutParams params, int widthHint, int heightHint) { this.fillLayoutParams(params, widthHint, heightHint); this.mPreservedParams.leftMargin = params.leftMargin; this.mPreservedParams.topMargin = params.topMargin; this.mPreservedParams.rightMargin = params.rightMargin; this.mPreservedParams.bottomMargin = params.bottomMargin; MarginLayoutParamsCompat.setMarginStart( this.mPreservedParams, MarginLayoutParamsCompat.getMarginStart(params)); MarginLayoutParamsCompat.setMarginEnd( this.mPreservedParams, MarginLayoutParamsCompat.getMarginEnd(params)); if (this.leftMarginPercent >= 0.0F) { params.leftMargin = (int) ((float) widthHint * this.leftMarginPercent); } if (this.topMarginPercent >= 0.0F) { params.topMargin = (int) ((float) heightHint * this.topMarginPercent); } if (this.rightMarginPercent >= 0.0F) { params.rightMargin = (int) ((float) widthHint * this.rightMarginPercent); } if (this.bottomMarginPercent >= 0.0F) { params.bottomMargin = (int) ((float) heightHint * this.bottomMarginPercent); } if (this.startMarginPercent >= 0.0F) { MarginLayoutParamsCompat.setMarginStart( params, (int) ((float) widthHint * this.startMarginPercent)); } if (this.endMarginPercent >= 0.0F) { MarginLayoutParamsCompat.setMarginEnd( params, (int) ((float) widthHint * this.endMarginPercent)); } if (Log.isLoggable("PercentLayout", 3)) { Log.d( "PercentLayout", "after fillMarginLayoutParams: (" + params.width + ", " + params.height + ")"); } }
// 删除状态图片 private void deleteNewsImageView(String tag) { // 删除 int subviewsCount = addImageLayout.getChildCount(); for (int i = 0; i < subviewsCount; i++) { View view = addImageLayout.getChildAt(i); if (null != view.getTag() && view.getTag().equals(tag)) { addImageLayout.removeViewAt(i); break; } } // 添加按钮位置重置 MarginLayoutParams addlp = (MarginLayoutParams) addImageView.getLayoutParams(); addlp.setMargins(oriMarginLeft, 0, 0, 0); // 删除之后重新排序 subviewsCount = addImageLayout.getChildCount(); for (int i = 1; i < subviewsCount; i++) { View view = addImageLayout.getChildAt(i); moveImageView((ImageView) view, i); } }
// 移动位置 private void moveImageView(ImageView imageView, int imageCount) { int columnNum = imageCount % 4; int lineNum = imageCount / 4; // 添加按钮位置 MarginLayoutParams addlp = (MarginLayoutParams) addImageView.getLayoutParams(); // LogUtils.i(columnNum+" "+lineNum+" "+addlp.leftMargin+" "+addImageView.getWidth()+" // "+addlp.width, // 1); int width = addlp.width; int height = addlp.height; // 布局位置 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height); lp.setMargins(addlp.leftMargin, addlp.topMargin, 0, 0); imageView.setLayoutParams(lp); addlp.setMargins(oriMarginLeft + (width + space) * columnNum, (height + 10) * lineNum, 0, 0); if (imageCount > 8) { addImageView.setVisibility(View.GONE); } else { addImageView.setVisibility(View.VISIBLE); } }
/* * Sets contact photo to the target imageview */ private void setContactPhoto(ImageView photoImageView, Bitmap contactPhoto) { if (contactPhoto == null) { setContactPhotoToDefault(photoImageView); return; } // Update background and padding if (SmsPopupUtils.PRE_ECLAIR) { photoImageView.setBackgroundResource(android.R.drawable.picture_frame); } else { photoImageView.setBackgroundResource(R.drawable.quickcontact_badge_small); } // Set margins for image MarginLayoutParams mLP = (MarginLayoutParams) photoImageView.getLayoutParams(); final int scaledMargin = (int) (contactPhotoMargin * this.getResources().getDisplayMetrics().density); mLP.setMargins(scaledMargin, scaledMargin, scaledMargin, scaledMargin); photoImageView.setLayoutParams(mLP); // Set contact photo image photoImageView.setImageBitmap(contactPhoto); }
public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; View listItem = null; for (int i = 0; i < listAdapter.getCount(); i++) { listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); listView.setLayoutParams(params); }
public void setContentX(final int contentX) { final MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams(); layoutParams.leftMargin = contentX; setLayoutParams(layoutParams); }
private void setInfoBottomMargin(int height) { MarginLayoutParams params = (MarginLayoutParams) indexText.getLayoutParams(); params.bottomMargin = height; indexText.setLayoutParams(params); }
public void setContentY(final int contentY) { final MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams(); layoutParams.topMargin = contentY; setLayoutParams(layoutParams); }
public void layout(Launcher launcher) { FrameLayout.LayoutParams lp; Resources res = launcher.getResources(); boolean hasVerticalBarLayout = isVerticalBarLayout(); // Layout the search bar space View searchBar = launcher.getSearchBar(); lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams(); if (hasVerticalBarLayout) { // Vertical search bar space lp.gravity = Gravity.TOP | Gravity.LEFT; lp.width = searchBarSpaceHeightPx; lp.height = LayoutParams.WRAP_CONTENT; LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar); targets.setOrientation(LinearLayout.VERTICAL); } else { // Horizontal search bar space lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; lp.width = searchBarSpaceWidthPx; lp.height = searchBarSpaceHeightPx; } searchBar.setLayoutParams(lp); // Layout the workspace PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace); lp = (FrameLayout.LayoutParams) workspace.getLayoutParams(); lp.gravity = Gravity.CENTER; int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT; Rect padding = getWorkspacePadding(orientation); workspace.setLayoutParams(lp); workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom); workspace.setPageSpacing(getWorkspacePageSpacing(orientation)); // Layout the hotseat View hotseat = launcher.findViewById(R.id.hotseat); lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams(); if (hasVerticalBarLayout) { // Vertical hotseat lp.gravity = Gravity.END; lp.width = hotseatBarHeightPx; lp.height = LayoutParams.MATCH_PARENT; hotseat.findViewById(R.id.layout).setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx); } else if (isTablet()) { // Pad the hotseat with the workspace padding calculated above lp.gravity = Gravity.BOTTOM; lp.width = LayoutParams.MATCH_PARENT; lp.height = hotseatBarHeightPx; hotseat.setPadding( edgeMarginPx + padding.left, 0, edgeMarginPx + padding.right, 2 * edgeMarginPx); } else { // For phones, layout the hotseat without any bottom margin // to ensure that we have space for the folders lp.gravity = Gravity.BOTTOM; lp.width = LayoutParams.MATCH_PARENT; lp.height = hotseatBarHeightPx; hotseat.findViewById(R.id.layout).setPadding(2 * edgeMarginPx, 0, 2 * edgeMarginPx, 0); } hotseat.setLayoutParams(lp); // Layout the page indicators View pageIndicator = launcher.findViewById(R.id.page_indicator); if (pageIndicator != null) { if (hasVerticalBarLayout) { // Hide the page indicators when we have vertical search/hotseat pageIndicator.setVisibility(View.GONE); } else { // Put the page indicators above the hotseat lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams(); lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; lp.width = LayoutParams.WRAP_CONTENT; lp.height = LayoutParams.WRAP_CONTENT; lp.bottomMargin = hotseatBarHeightPx; pageIndicator.setLayoutParams(lp); } } // Layout AllApps AppsCustomizeTabHost host = (AppsCustomizeTabHost) launcher.findViewById(R.id.apps_customize_pane); if (host != null) { // Center the all apps page indicator int pageIndicatorHeight = (int) (pageIndicatorHeightPx * Math.min(1f, (allAppsIconSizePx / DynamicGrid.DEFAULT_ICON_SIZE_PX))); pageIndicator = host.findViewById(R.id.apps_customize_page_indicator); if (pageIndicator != null) { LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) pageIndicator.getLayoutParams(); lllp.width = LayoutParams.WRAP_CONTENT; lllp.height = pageIndicatorHeight; pageIndicator.setLayoutParams(lllp); } AppsCustomizePagedView pagedView = (AppsCustomizePagedView) host.findViewById(R.id.apps_customize_pane_content); FrameLayout fakePageContainer = (FrameLayout) host.findViewById(R.id.fake_page_container); FrameLayout fakePage = (FrameLayout) host.findViewById(R.id.fake_page); padding = new Rect(); if (pagedView != null) { // Constrain the dimensions of all apps so that it does not span the full width int paddingLR = (availableWidthPx - (allAppsCellWidthPx * allAppsNumCols)) / (2 * (allAppsNumCols + 1)); int paddingTB = (availableHeightPx - (allAppsCellHeightPx * allAppsNumRows)) / (2 * (allAppsNumRows + 1)); paddingLR = Math.min(paddingLR, (int) ((paddingLR + paddingTB) * 0.75f)); paddingTB = Math.min(paddingTB, (int) ((paddingLR + paddingTB) * 0.75f)); int maxAllAppsWidth = (allAppsNumCols * (allAppsCellWidthPx + 2 * paddingLR)); int gridPaddingLR = (availableWidthPx - maxAllAppsWidth) / 2; // Only adjust the side paddings on landscape phones, or tablets if ((isTablet() || isLandscape) && gridPaddingLR > (allAppsCellWidthPx / 4)) { padding.left = padding.right = gridPaddingLR; } // The icons are centered, so we can't just offset by the page indicator height // because the empty space will actually be pageIndicatorHeight + paddingTB padding.bottom = Math.max(0, pageIndicatorHeight - paddingTB); pagedView.setWidgetsPageIndicatorPadding(pageIndicatorHeight); fakePage.setBackground(res.getDrawable(R.drawable.quantum_panel)); // Horizontal padding for the whole paged view int pagedFixedViewPadding = res.getDimensionPixelSize(R.dimen.apps_customize_horizontal_padding); padding.left += pagedFixedViewPadding; padding.right += pagedFixedViewPadding; pagedView.setPadding(padding.left, padding.top, padding.right, padding.bottom); fakePageContainer.setPadding(padding.left, padding.top, padding.right, padding.bottom); } } // Layout the Overview Mode ViewGroup overviewMode = launcher.getOverviewPanel(); if (overviewMode != null) { Rect r = getOverviewModeButtonBarRect(); lp = (FrameLayout.LayoutParams) overviewMode.getLayoutParams(); lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; int visibleChildCount = getVisibleChildCount(overviewMode); int totalItemWidth = visibleChildCount * overviewModeBarItemWidthPx; int maxWidth = totalItemWidth + (visibleChildCount - 1) * overviewModeBarSpacerWidthPx; lp.width = Math.min(availableWidthPx, maxWidth); lp.height = r.height(); overviewMode.setLayoutParams(lp); if (lp.width > totalItemWidth && visibleChildCount > 1) { // We have enough space. Lets add some margin too. int margin = (lp.width - totalItemWidth) / (visibleChildCount - 1); View lastChild = null; // Set margin of all visible children except the last visible child for (int i = 0; i < visibleChildCount; i++) { if (lastChild != null) { MarginLayoutParams clp = (MarginLayoutParams) lastChild.getLayoutParams(); if (isLayoutRtl) { clp.leftMargin = margin; } else { clp.rightMargin = margin; } lastChild.setLayoutParams(clp); lastChild = null; } View thisChild = overviewMode.getChildAt(i); if (thisChild.getVisibility() != View.GONE) { lastChild = thisChild; } } } } }