Encode the layout_gravity, gravity, and layoutgravity of the FrameLayout sub-View.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.