Android gets the width and height of the control in OnCreate. androidoncreate
A demo. in xml layout, the height of a row is half that of other rows. Previously, layout_weight was used to produce unsatisfactory results.
<?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="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000" android:layout_weight="2" > <TextView android: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" > <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_parent" android:layout_height="wrap_content"android:background="#ddaacc" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textColor="@android:color/black" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"android:background="#000" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout>
Although it works, if there is too much content in it, this layout will not work. Therefore, we dynamically set the height of the mobile phone screen based on its size.
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 the width and height of homelayout 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 ;}});}