Android中LayoutParams的用法

來源:互聯網
上載者:User

簡單說說 自己對 android LayoutParams的理解吧,xh寫不出進階文章是低級寫手。
public static class
ViewGroup.LayoutParams
extends Object

java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams   //繼承關係

以下說明摘自官方文檔E文好的可以看看
Class Overview

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports.

The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
an exact number
There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.

E文不好看不懂  但是覺得寫得囉嗦了
其實這個LayoutParams類是用於child view(子視圖) 向 parent view(父視圖)傳達自己的意願的一個東西(孩子想變成什麼樣向其父親說明)其實子視圖父視圖可以簡單理解成
一個LinearLayout 和 這個LinearLayout裡邊一個 TextView 的關係 TextView 就算LinearLayout的子視圖 child view 。需要注意的是LayoutParams只是ViewGroup的一個內部類這裡邊這個也就是ViewGroup裡邊這個LayoutParams類是 base class 基類實際上每個不同的ViewGroup都有自己的LayoutParams子類
比如LinearLayout 也有自己的 LayoutParams 大家開啟源碼看幾眼就知道了
myeclipse 怎麼查看源碼 請看http://byandby.iteye.com/blog/814277
下邊來個例子

 

Java代碼 :
  1.       //建立一個線性布局
      
  2.        private LinearLayout mLayout;      
  3.        mLayout = (LinearLayout) findViewById(R.id.layout);      
  4.       //現在我要往mLayout裡邊添加一個TextView 
      
  5.      //你可能會想直接在布局檔案裡邊配置不就O 了 那是 但是這裡為了說明問題我們用代碼實現
      
  6.       TextView textView = new TextView(Activity01.this);   
      
  7.            textView.setText("Text View " );   
  8.            //這裡請不要困惑這裡是設定 這個textView的布局 FILL_PARENT WRAP_CONTENT 和在xml檔案裡邊設定是一樣的如
      
  9.     
  10. //在xml裡邊怎麼配置高寬大家都會的。   
  11.   //第一個參數為寬的設定,第二個參數為高的設定。
      
  12.            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(      
  13.                    LinearLayout.LayoutParams.FILL_PARENT,      
  14.                    LinearLayout.LayoutParams.WRAP_CONTENT      
  15.            );      
  16.            //調用addView()方法增加一個TextView到線性布局中
      
  17.            mLayout.addView(textView, p);      
  18.           //比較簡單的一個例子  

如果還不能理解下邊在來一段直白的說明:
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,即包裹住組件就好。

關於setLayoutParams報錯

  在繼承BaseAdapter的時候,用getView返回View的時候,用代碼控制布局,需要用到View.setLayoutParams,但是報錯了,報的是類型轉換錯誤,經過研究,發現,這裡不能使用ViewGroup.LayoutParams而必須使用對應父View的LayoutParams類型。如:某View被LinearLayout包含,則該View的setLayoutParams參數類型必須是LinearLayout.LayoutParams。原因在於LinearLayout(或其他繼承自ViewGroup的layout,如:RelativeLayout)在進行遞迴布局的時候,LinearLayout會擷取子View的LayoutParams,並強制轉換成LinearLayout.LayoutParams,如

1 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();

或者是如下定義:

1 LayoutParams lp = (LayoutParams) child.getLayoutParams();

以轉換成內部類型LinearLayout.LayoutParams。

 

相關文章

聯繫我們

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