關於LayoutParams 的學習

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 大家打開源碼看幾眼就知道了
下邊來個例子

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.   /**<TextView  
  10.            android:layout_width="fill_parent"  
  11.            android:layout_height="wrap_content"  
  12.            android:text="Text View"/>*/  
  13. //在xml裏邊怎麼配置高寬大家都會的。   
  14.   //第一個參數爲寬的設置,第二個參數爲高的設置。   
  15.            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(      
  16.                    LinearLayout.LayoutParams.FILL_PARENT,      
  17.                    LinearLayout.LayoutParams.WRAP_CONTENT      
  18.            );      
  19.            //調用addView()方法增加一個TextView到線性佈局中   
  20.            mLayout.addView(textView, p);      
  21.           //比較簡單的一個例子  
       //創建一個線性佈局
        private LinearLayout mLayout;   
        mLayout = (LinearLayout) findViewById(R.id.layout);   
       //現在我要往mLayout裏邊添加一個TextView 
      //你可能會想直接在佈局文件裏邊配置不就O 了 那是 但是這裏爲了說明問題我們用代碼實現
       TextView textView = new TextView(Activity01.this);   
            textView.setText("Text View " );
            //這裏請不要困惑這裏是設置 這個textView的佈局 FILL_PARENT WRAP_CONTENT 和在xml文件裏邊設置是一樣的如
   /**<TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Text View"/>*/
 //在xml裏邊怎麼配置高寬大家都會的。
   //第一個參數爲寬的設置,第二個參數爲高的設置。
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(   
                    LinearLayout.LayoutParams.FILL_PARENT,   
                    LinearLayout.LayoutParams.WRAP_CONTENT   
            );   
            //調用addView()方法增加一個TextView到線性佈局中
            mLayout.addView(textView, p);   
           //比較簡單的一個例子


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

轉自:http://byandby.javaeye.com/blog/816718  各篇文章都是爲了方便學習轉載。

發佈了15 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章