下面列出了怎么用android.widget.FrameLayout.LayoutParams的API类实例代码及写法,或者点击链接到github查看源代码。
public void init() {
this.mVideoWidth = 0;
this.mVideoHeight = 0;
setBackgroundColor(ViewCompat.MEASURED_STATE_MASK);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
this.mCurrentState = 0;
this.mTargetState = 0;
TextureView renderUIView = new TextureView(getContext());
renderUIView.setLayoutParams(new LayoutParams(-2, -2, 17));
renderUIView.setSurfaceTextureListener(this.mSurfaceCallback);
this.mX8Camera9GridView = new X8Camera9GridView(getContext());
this.mX8Camera9GridView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.mX8AiTrackContainterView = new X8AiTrackContainterView(getContext());
this.mX8AiTrackContainterView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.blackView = new View(getContext());
this.blackView.setLayoutParams(new LayoutParams(-1, -1, 17));
this.blackView.setBackgroundColor(getContext().getResources().getColor(R.color.black));
addView(renderUIView);
addView(this.mX8AiTrackContainterView);
addView(this.blackView);
addView(this.mX8Camera9GridView);
showGridLine(GlobalConfig.getInstance().getGridLine());
}
/**
* Gloading(loading status view) wrap the specific view.
* @param view view to be wrapped
* @return Holder
*/
public Holder wrap(View view) {
FrameLayout wrapper = new FrameLayout(view.getContext());
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null) {
wrapper.setLayoutParams(lp);
}
if (view.getParent() != null) {
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
parent.addView(wrapper, index);
}
LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
wrapper.addView(view, newLp);
return new Holder(mAdapter, view.getContext(), wrapper);
}
/**
* Set the anchor view to specified position and size (all units in dp).
* @param view The anchor view that needs to be positioned.
* @param x X coordinate of the top left corner of the anchor view.
* @param y Y coordinate of the top left corner of the anchor view.
* @param width The width of the anchor view.
* @param height The height of the anchor view.
*/
@CalledByNative
public void setViewPosition(View view, float x, float y,
float width, float height, float scale, int leftMargin, int topMargin) {
ViewGroup containerView = getContainerView();
if (containerView == null) return;
int scaledWidth = Math.round(width * scale);
int scaledHeight = Math.round(height * scale);
int startMargin;
if (ApiCompatibilityUtils.isLayoutRtl(containerView)) {
startMargin = containerView.getMeasuredWidth() - Math.round((width + x) * scale);
} else {
startMargin = leftMargin;
}
if (scaledWidth + startMargin > containerView.getWidth()) {
scaledWidth = containerView.getWidth() - startMargin;
}
LayoutParams lp = new LayoutParams(scaledWidth, scaledHeight);
ApiCompatibilityUtils.setMarginStart(lp, startMargin);
lp.topMargin = topMargin;
view.setLayoutParams(lp);
}
/**
* Gloading(loading status view) wrap the specific view.
* @param view view to be wrapped
* @return Holder
*/
public Holder wrap(View view) {
FrameLayout wrapper = new FrameLayout(view.getContext());
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null) {
wrapper.setLayoutParams(lp);
}
if (view.getParent() != null) {
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
parent.addView(wrapper, index);
}
LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
wrapper.addView(view, newLp);
return new Holder(mAdapter, view.getContext(), wrapper);
}
private DialogCropPhotoTransit(Context context) {
super(context, R.style.dialogCropPhotoTransit);
toShowAnimation = true;
this.setCancelable(false);
this.mContext = context;
this.mWindow = this.getWindow();
this.mWindowLp = mWindow.getAttributes();
mWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mWindowLp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mWindowLp.windowAnimations = R.style.dialogCropPhotoTransit;
mWindow.setAttributes(mWindowLp);
this.setContentView(R.layout.dialog_crop_image_transit);
this.mIv = (ImageView) findViewById(R.id.iv_photo);
ivLp = (LayoutParams) mIv.getLayoutParams();
mFl = (FrameLayout) findViewById(R.id.fl_container);
mFl.getLayoutParams().height = mContext.getResources()
.getDisplayMetrics().heightPixels;
mFl.getLayoutParams().width = mContext.getResources()
.getDisplayMetrics().widthPixels;
}
public BasicNativePage(Activity activity, Tab tab) {
initialize(activity, tab);
mActivity = activity;
mTab = tab;
mBackgroundColor = ApiCompatibilityUtils.getColor(activity.getResources(),
R.color.default_primary_color);
mThemeColor = ApiCompatibilityUtils.getColor(
activity.getResources(), R.color.default_primary_color);
Resources res = mActivity.getResources();
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0,
res.getDimensionPixelSize(R.dimen.tab_strip_height)
+ res.getDimensionPixelSize(R.dimen.toolbar_height_no_shadow),
0, 0);
getView().setLayoutParams(layoutParams);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
FrameLayout root = new FrameLayout(context);
LinearLayout pframe = new LinearLayout(context);
pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
pframe.setOrientation(1);
pframe.setVisibility(8);
pframe.setGravity(17);
pframe.addView(new ProgressBar(context, null, 16842874), new LayoutParams(-2, -2));
root.addView(pframe, new LayoutParams(-1, -1));
FrameLayout lframe = new FrameLayout(context);
lframe.setId(INTERNAL_LIST_CONTAINER_ID);
TextView tv = new TextView(getActivity());
tv.setId(INTERNAL_EMPTY_ID);
tv.setGravity(17);
lframe.addView(tv, new LayoutParams(-1, -1));
ListView lv = new ListView(getActivity());
lv.setId(16908298);
lv.setDrawSelectorOnTop(false);
lframe.addView(lv, new LayoutParams(-1, -1));
root.addView(lframe, new LayoutParams(-1, -1));
root.setLayoutParams(new LayoutParams(-1, -1));
return root;
}
/**
* Inflates the view, which is associated with a tab, and adds it to the view hierarchy.
*
* @param tabItem
* The tab item, which corresponds to the tab, whose associated view should be inflated,
* as an instance of the class {@link TabItem}. The tab item may not be null
*/
private void addContentView(@NonNull final TabItem tabItem) {
PhoneTabViewHolder viewHolder = (PhoneTabViewHolder) tabItem.getViewHolder();
View view = viewHolder.content;
Tab tab = tabItem.getTab();
if (view == null) {
ViewGroup parent = viewHolder.contentContainer;
Pair<View, ?> pair = tabViewRecycler.inflate(tab, parent);
view = pair.first;
LayoutParams layoutParams =
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Rect padding = getPadding();
layoutParams.setMargins(padding.left, padding.top, padding.right, padding.bottom);
parent.addView(view, 0, layoutParams);
viewHolder.content = view;
} else {
tabViewRecycler.getAdapter().onShowView(getModel().getContext(), view, tab, false);
}
viewHolder.previewImageView.setVisibility(View.GONE);
viewHolder.previewImageView.setImageBitmap(null);
viewHolder.borderView.setVisibility(View.GONE);
}
@Override
protected final void onShowTabView(@NonNull final View view, @NonNull final TabItem tabItem,
@NonNull final Integer... params) {
LayoutParams layoutParams =
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
int borderMargin = -(tabInset + tabBorderWidth);
int bottomMargin = params.length > 0 && params[0] != -1 ? params[0] : borderMargin;
layoutParams.leftMargin = borderMargin;
layoutParams.topMargin = -(tabInset + tabTitleContainerHeight);
layoutParams.rightMargin = borderMargin;
layoutParams.bottomMargin = bottomMargin;
view.setLayoutParams(layoutParams);
adaptContentBackgroundColor(tabItem);
adaptBackgroundVisibility(tabItem);
if (!getModel().isSwitcherShown()) {
addContentView(tabItem);
} else {
renderPreview(tabItem);
}
}
private void hideBottomBar() {
if (mBottomBarView == null) return;
((ViewGroup) mBottomBarView.getParent()).removeView(mBottomBarView);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
final ViewGroup compositorView = mActivity.getCompositorViewHolder();
compositorView.addView(mBottomBarView, lp);
compositorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
compositorView.removeOnLayoutChangeListener(this);
mBottomBarView.animate().alpha(0f).translationY(mBottomBarView.getHeight())
.setInterpolator(BakedBezierInterpolator.TRANSFORM_CURVE)
.setDuration(SLIDE_ANIMATION_DURATION_MS)
.withEndAction(new Runnable() {
@Override
public void run() {
((ViewGroup) mBottomBarView.getParent()).removeView(mBottomBarView);
mBottomBarView = null;
}
}).start();
}
});
}
@Override
protected MeasureOutput measure(int width, int height) {
MeasureOutput measureOutput = new MeasureOutput();
if (this.mOrientation == WXVContainer.HORIZONTAL) {
int screenW = WXViewUtils.getScreenWidth(WXEnvironment.sApplication);
int weexW = WXViewUtils.getWeexWidth(mInstanceId);
measureOutput.width = width > (weexW >= screenW ? screenW : weexW) ? FrameLayout.LayoutParams.MATCH_PARENT
: width;
measureOutput.height = height;
} else {
int screenH = WXViewUtils.getScreenHeight(WXEnvironment.sApplication);
int weexH = WXViewUtils.getWeexHeight(mInstanceId);
measureOutput.height = height > (weexH >= screenH ? screenH : weexH) ? FrameLayout.LayoutParams.MATCH_PARENT
: height;
measureOutput.width = width;
}
return measureOutput;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
FrameLayout fl = new FrameLayout(getActivity());
fl.setLayoutParams(params);
final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
.getDisplayMetrics());
TextView v = new TextView(getActivity());
params.setMargins(margin, margin, margin, margin);
v.setLayoutParams(params);
v.setGravity(Gravity.CENTER);
v.setBackgroundResource(R.drawable.view_sliding_tab_background_card);
v.setText("CARD " + (position + 1));
fl.addView(v);
return fl;
}
/**
* StateViewLayout(loading status view) wrap the specific view.
* @param view view to be wrapped
* @return Holder
*/
public Holder wrap(View view) {
FrameLayout wrapper = new FrameLayout(view.getContext());
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null) {
wrapper.setLayoutParams(lp);
}
if (view.getParent() != null) {
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
parent.addView(wrapper, index);
}
LayoutParams newLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
wrapper.addView(view, newLp);
return new Holder(mAdapter, view.getContext(), wrapper);
}
private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
params.gravity = Gravity.TOP;
if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
params.rightMargin = mConfig.getNavigationBarWidth();
}
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mStatusBarTintView);
}
public void changeTrackView(int w, int h) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) this.mX8AiTrackContainterView.getLayoutParams();
lp.width = w;
lp.height = h;
this.mX8AiTrackContainterView.setLayoutParams(lp);
this.mX8AiTrackContainterView.isFullScreen(w > 800);
}
private TextView newTextView() {
TextView textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(-1, -1, 16));
textView.setCompoundDrawablePadding(10);
textView.setPadding(10, 0, 0, 0);
textView.setGravity(16);
textView.setLines(2);
textView.setEllipsize(TruncateAt.END);
textView.setTextColor(Color.parseColor("#FFFFFF"));
textView.setTextSize(1, 14.0f);
return textView;
}
public void setRenderView(IRenderView renderView) {
View renderUIView;
if (this.mRenderView != null) {
if (this.mMediaPlayer != null) {
this.mMediaPlayer.setDisplay(null);
}
renderUIView = this.mRenderView.getView();
this.mRenderView.removeRenderCallback(this.mSHCallback);
this.mRenderView = null;
removeView(renderUIView);
}
if (renderView != null) {
this.mRenderView = renderView;
renderView.setAspectRatio(this.mCurrentAspectRatio);
if (this.mVideoWidth > 0 && this.mVideoHeight > 0) {
renderView.setVideoSize(this.mVideoWidth, this.mVideoHeight);
}
if (this.mVideoSarNum > 0 && this.mVideoSarDen > 0) {
renderView.setVideoSampleAspectRatio(this.mVideoSarNum, this.mVideoSarDen);
}
renderUIView = this.mRenderView.getView();
renderUIView.setLayoutParams(new LayoutParams(-2, -2, 17));
addView(renderUIView);
this.mRenderView.addRenderCallback(this.mSHCallback);
this.mRenderView.setVideoRotation(this.mVideoRotationDegree);
}
}
private static void addMarginTopToContentChild(View mContentChild, int statusBarHeight) {
if (mContentChild != null && !TAG_MARGIN_ADDED.equals(mContentChild.getTag())) {
LayoutParams lp = (LayoutParams) mContentChild.getLayoutParams();
lp.topMargin += statusBarHeight;
mContentChild.setLayoutParams(lp);
mContentChild.setTag(TAG_MARGIN_ADDED);
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
initializeThumbnailView();
initializePlayerView();
FrameLayout viewFrame = new FrameLayout(getActivity());
viewFrame.addView(mThumbnailView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
viewFrame.addView(mPlayerView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
return viewFrame;
}
public void setTargetView(View target) {
if (getParent() != null) {
((ViewGroup) getParent()).removeView(this);
}
if (target == null) {
return;
}
if (target.getParent() instanceof FrameLayout) {
((FrameLayout) target.getParent()).addView(this);
} else if (target.getParent() instanceof ViewGroup) {
// use a new Framelayout container for adding badge
ViewGroup parentContainer = (ViewGroup) target.getParent();
int groupIndex = parentContainer.indexOfChild(target);
parentContainer.removeView(target);
FrameLayout badgeContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams parentLayoutParams = target.getLayoutParams();
badgeContainer.setLayoutParams(parentLayoutParams);
target.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentContainer.addView(badgeContainer, groupIndex, parentLayoutParams);
badgeContainer.addView(target);
badgeContainer.addView(this);
} else if (target.getParent() == null) {
Log.e(getClass().getSimpleName(), "ParentView is needed");
}
}
public void setRenderView(IRenderView renderView) {
View renderUIView;
if (this.mRenderView != null) {
if (this.mMediaPlayer != null) {
this.mMediaPlayer.setDisplay(null);
}
renderUIView = this.mRenderView.getView();
this.mRenderView.removeRenderCallback(this.mSHCallback);
this.mRenderView = null;
removeView(renderUIView);
}
if (renderView != null) {
this.mRenderView = renderView;
renderView.setAspectRatio(this.mCurrentAspectRatio);
if (this.mVideoWidth > 0 && this.mVideoHeight > 0) {
renderView.setVideoSize(this.mVideoWidth, this.mVideoHeight);
}
if (this.mVideoSarNum > 0 && this.mVideoSarDen > 0) {
renderView.setVideoSampleAspectRatio(this.mVideoSarNum, this.mVideoSarDen);
}
renderUIView = this.mRenderView.getView();
renderUIView.setLayoutParams(new LayoutParams(-2, -2, 17));
addView(renderUIView);
this.mRenderView.addRenderCallback(this.mSHCallback);
this.mRenderView.setVideoRotation(this.mVideoRotationDegree);
}
}
private void init() {
if (!(getLayoutParams() instanceof LayoutParams)) {
LayoutParams layoutParams =
new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
setLayoutParams(layoutParams);
}
// set default font
setTextColor(Color.WHITE);
//setTypeface(Typeface.DEFAULT_BOLD);
setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
setPadding(dip2Px(5), dip2Px(1), dip2Px(5), dip2Px(1));
radius = 8;
// set default background
setBackground(radius, Color.parseColor("#d3321b"));
setGravity(Gravity.CENTER);
// default values
setHideOnNull(true);
setBadgeCount(0);
setMinWidth(dip2Px(16));
setMinHeight(dip2Px(16));
}
private void setupNavBarView(Context context, ViewGroup decorViewGroup) {
mNavBarTintView = new View(context);
LinearLayout.LayoutParams params;
if (mConfig.isNavigationAtBottom()) {
params = generateLayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, mConfig.getNavigationBarHeight(),0);
} else {
params = generateLayoutParams(mConfig.getNavigationBarWidth(),
LinearLayout.LayoutParams.MATCH_PARENT,0);
}
mNavBarTintView.setLayoutParams(params);
mNavBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mNavBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mNavBarTintView);
}
public void setFakeHeaderHeight(int height)
{
this.fakeHeaderHeight = height;
if (fakeHeaderView != null)
{
android.view.ViewGroup.LayoutParams params = fakeHeaderView.getLayoutParams();
params.height = height;
fakeHeaderView.setLayoutParams(params);
}
}
private void setLayoutParams() {
if(!(getLayoutParams() instanceof LayoutParams)){
LayoutParams lParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.END | Gravity.TOP);
setLayoutParams(lParams);
}
}
/**
* 设置红点依附的view
* @param view view
*/
public void setTargetView(View view){
if (getParent() != null) {
((ViewGroup) getParent()).removeView(this);
}
if (view == null) {
return;
}
if(view.getParent() instanceof FrameLayout){
((FrameLayout) view.getParent()).addView(this);
}else if(view.getParent() instanceof ViewGroup){
ViewGroup parentContainer = (ViewGroup) view.getParent();
int groupIndex = parentContainer.indexOfChild(view);
parentContainer.removeView(view);
FrameLayout badgeContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams parentLayoutParams = view.getLayoutParams();
badgeContainer.setLayoutParams(parentLayoutParams);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentContainer.addView(badgeContainer, groupIndex, parentLayoutParams);
badgeContainer.addView(view);
badgeContainer.addView(this);
}else {
Log.e(getClass().getSimpleName(), "ParentView is must needed");
}
}
private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
params.gravity = Gravity.TOP;
if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
params.rightMargin = mConfig.getNavigationBarWidth();
}
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mStatusBarTintView);
}
public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {
LayoutParams params = (LayoutParams) getLayoutParams();
params.leftMargin = dip2Px(leftDipMargin);
params.topMargin = dip2Px(topDipMargin);
params.rightMargin = dip2Px(rightDipMargin);
params.bottomMargin = dip2Px(bottomDipMargin);
setLayoutParams(params);
}
private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
params.gravity = Gravity.TOP;
if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
params.rightMargin = mConfig.getNavigationBarWidth();
}
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
mStatusBarTintView.setVisibility(View.GONE);
decorViewGroup.addView(mStatusBarTintView);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("测试插件中的FragmentActivity");
FrameLayout root = new FrameLayout(this);
setContentView(root, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
root.setId(android.R.id.primary);
Fragment fragment = new PluginNormalFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.primary, fragment).commit();
}