Android Gets the width and height of the control in OnCreate

Source: Internet
Author: User

A demo, in an XML layout, a row is half the height of another row. Before using Layout_weight, the results are also not ideal.

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" ver Tical "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_cont Ent "android:background=" #ff0000 "android:layout_weight=" 2 "> <textview Androi D:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "1" android:textcolor = "@android: Color/white"/> </LinearLayout> <linearlayout android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:background=" #cccccc "android:layout_weight=" 2 "&G        T <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "2 "Android:textcoLor= "@android: Color/black"/> </LinearLayout> <linearlayout android:layout_width= "Match_pare        NT "android:layout_height=" Wrap_content "android:background=" #ddaacc "android:layout_weight=" 1 "> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:t         ext= "3" android:textcolor= "@android: Color/black"/> </LinearLayout> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:background= "#000" Android oid:layout_weight= "1" > <textview android:layout_width= "wrap_content" Android:layout_ height= "Wrap_content" android:text= "4" android:textcolor= "@android: Color/white"/> </line  Arlayout> </LinearLayout>

It works, but if there's too much content, the layout doesn't work. So we set its height dynamically based on the size of the phone screen.

<pre name= "code" class= "Java" >public class Homeactivity extends Activity{private linearlayout homelayout;// Parent private LinearLayout home1;private linearlayout home2;private linearlayout home3;private LinearLayout Home4;boolean Hasmeasured = false; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_home); home1 = (linearlayout) Findviewbyid (r.id.home_content_section1); home2 = ( LinearLayout) Findviewbyid (r.id.home_content_section2); Home3 = (linearlayout) Findviewbyid (r.id.home_content_ SECTION3); home4 = (linearlayout) Findviewbyid (r.id.home_content_section4); homelayout = (linearlayout) Findviewbyid ( R.id.home_content); Viewtreeobserver vto = Homelayout.getviewtreeobserver (); Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {@Overridepublic Boolean onpredraw () {if (hasmeasured = = False) {//Get H                Width and height of omelayout int height = homelayout.getmeasuredheight (); Int width = Homelayout.getmeasuredwidth ();                Android.view.ViewGroup.LayoutParams LP1 =home1.getlayoutparams ();                LP1.HEIGHT=HEIGHT*2/7;                Android.view.ViewGroup.LayoutParams LP2 =home2.getlayoutparams ();                LP2.HEIGHT=HEIGHT*2/7;                Android.view.ViewGroup.LayoutParams lp3 =home3.getlayoutparams ();                LP3.HEIGHT=HEIGHT*1/7;                Android.view.ViewGroup.LayoutParams LP4 =home4.getlayoutparams ();                LP4.HEIGHT=HEIGHT*2/7;                Hasmeasured = true; }return true;}});}

Android Gets the width and height of the control in OnCreate

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.