android在OnCreate中擷取控制項的寬度和高度

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   color   sp   java   on   

一個demo,在xml布局中,某一行的高度為其他行的一半。之前用layout_weight,得出的效果也不太理想。

<?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> 

儘管有效果了,但是如果裡面的內容過多的話,這個布局就不起作用了。所以我們根據手機螢幕的大小  動態設定它的高度。

<pre name="code" class="java">public class HomeActivity extends Activity{private LinearLayout homelayout; //父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)                {//擷取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;}});}


android在OnCreate中擷取控制項的寬度和高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.