Encode the layout_gravity, gravity, and layoutgravity of the FrameLayout sub-View.
Method:
SetLayoutParams: Used to set attributes with the layout prefix. All child views attach to the parent View will have LayoutParams, but before parentView. addView (childView), childView. getLayoutParams () will return null.
Based on the source code:
public void setLayoutParams(ViewGroup.LayoutParams params) { if (params == null) { throw new NullPointerException("Layout parameters cannot be null"); } mLayoutParams = params; resolveLayoutParams(); if (mParent instanceof ViewGroup) { ((ViewGroup) mParent).onSetLayoutParams(this, params); } requestLayout(); }
We can see that to make setlayoutParams take effect, a parent View must be available, that is, addView is called.
SetGravity: Used to set internal sub-views.
Code:
mTitleBar = new LinearLayout(context); mTitleBar.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200)); mTitleBar.setBackgroundColor(Color.BLACK); TextView textView = new TextView(context); textView.setText("lfjasodijfaodjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); mTitleBar.addView(textView); LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(textParams); textView.setGravity(Gravity.END); mBottomBar = new LinearLayout(context); TextView bottomTV = new TextView(context); bottomTV.setText("lfjasodijfaodjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"); mBottomBar.addView(bottomTV); this.addView(mTitleBar); this.addView(mBottomBar); FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 70); frameLayoutParams.gravity = Gravity.TOP; mTitleBar.setLayoutParams(frameLayoutParams); FrameLayout.LayoutParams frameLayoutParams2 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 70); frameLayoutParams2.gravity = Gravity.BOTTOM; mBottomBar.setLayoutParams(frameLayoutParams2);
Effect:
Two textviews. In two LinearLayout, the above TextView gravity is on the right. Two LinearyLayout layout_gravity in FrameLayout, one Top and one Bottom
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.