Android項目---LayoutParams

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

LinearLayout.LayoutParams

extends ViewGroup.MarginLayoutParams

java.lang.Object
   ? android.view.ViewGroup.LayoutParams
     ? android.view.ViewGroup.MarginLayoutParams
       ? android.widget.LinearLayout.LayoutParams
Known Direct Subclasses RadioGroup.LayoutParams, TableLayout.LayoutParams, TableRow.LayoutParams
  

LayoutParams繼承於Android.View.ViewGroup.LayoutParams.
       LayoutParams相當於一個Layout的資訊包,它封裝了Layout的位置、高、寬等資訊。假設在螢幕上一塊地區是由一個Layout佔領的,如果將一個View添加到一個Layout中,最好告訴Layout使用者期望的布局方式,也就是將一個認可的layoutParams傳遞進去。
       可以這樣去形容LayoutParams,在象棋的棋盤上,每個棋子都佔據一個位置,也就是每個棋子都有一個位置的資訊,如這個棋子在4行4列,這裡的“4行4列”就是棋子的LayoutParams。

       但LayoutParams類也只是簡單的描述了寬高,寬和高都可以設定成三種值:
       1,一個確定的值;
       2,FILL_PARENT,即填滿(和父容器一樣大小);
       3,WRAP_CONTENT,即包裹住組件就好。

    /**     * 擷取螢幕的方法     */    private void MyWindowWidth() {        // 擷取螢幕的寬度        WindowManager wm = (WindowManager) HomeActivity.this                .getSystemService(Context.WINDOW_SERVICE);        // 擷取預設顯示螢幕寬度        int width = wm.getDefaultDisplay().getWidth();// 取出螢幕的寬度        // 擷取layout左邊的螢幕的參數屬性        LinearLayout.LayoutParams lp = (LayoutParams) left_menu                .getLayoutParams();        // 設定左邊螢幕寬度屬性為預設螢幕寬度的 8/10        lp.width = width / 10 * 8;        // 設定左邊螢幕的位置屬性為左邊距的 -8/10        lp.leftMargin = -width / 10 * 8;        // 將設定好的layout左邊螢幕的參數屬性放到功能表列上        left_menu.setLayoutParams(lp);        // 獲得ViewGroup的螢幕的參數屬性        ViewGroup.LayoutParams lp2 = (LayoutParams) viewgroup.getLayoutParams();        // 設定ViewGroup的螢幕寬度為預設寬度        lp2.width = width;        // 將設定好的右邊viewgroup螢幕的參數屬性放到自訂布局上        viewgroup.setLayoutParams(lp2);        // 獲得螢幕中titile的參數屬性        lp3 = (LayoutParams) title.getLayoutParams();        // 給titile的寬度設定為螢幕的預設寬度        lp3.width = width;    }

向viewGroup中添加View

viewgroup.addView(localActivityManager.getActivity(activityName)                .getWindow().getDecorView(), new LayoutParams(                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.