使用LayoutParams設(shè)置布局
1、public static class ViewGroup.LayoutParams extends Object ?
java.lang.Object
???? android.view.ViewGroup.LayoutParams ? Class Overview
LayoutParams are used by views to tell their parents how they want to be laid out.
LayoutParams是ViewGroup的一個內(nèi)部類, 每個不同的ViewGroup都有自己的LayoutParams子類
DEMO:
//創(chuàng)建一個線性布局????
private?LinearLayout?mLayout;????
mLayout?=?(LinearLayout)?findViewById(R.id.layout);?
//現(xiàn)在要往mLayout里邊添加一個TextView????
????
TextView?textView?=?new?TextView(Activity01.this);????
textView.setText("Text?View?"?);????
//這里是設(shè)置?這個textView的布局?FILL_PARENT?WRAP_CONTENT?和在xml文件里邊設(shè)置是一樣的如????
/***/
????
//第一個參數(shù)為寬的設(shè)置,第二個參數(shù)為高的設(shè)置。????
LinearLayout.LayoutParams?p?=?new?LinearLayout.LayoutParams(????
LinearLayout.LayoutParams.FILL_PARENT,????
LinearLayout.LayoutParams.WRAP_CONTENT????
);????
//調(diào)用addView()方法增加一個TextView到線性布局中????
mLayout.addView(textView,?p);????
//比較簡單的一個例子
LayoutParams繼承于Android.View.ViewGroup.LayoutParams.
LayoutParams相當于一個Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設(shè)在屏幕上一塊區(qū)域是由一個Layout占領(lǐng)的,如果將一個View添加到一個Layout中,最好告訴Layout用戶期望的布局方式,也就是將一個認可的layoutParams傳遞進去。
可以這樣去形容LayoutParams,在象棋的棋盤上,每個棋子都占據(jù)一個位置,也就是每個棋子都有一個位置的信息,如這個棋子在4行4列,這里的“4行4列”就是棋子的LayoutParams。
但LayoutParams類也只是簡單的描述了寬高,寬和高都可以設(shè)置成三種值:
1,一個確定的值;
2,F(xiàn)ILL_PARENT
3,WRAP_CONTENT
2、addRule() public class RelativeLayout extends ViewGroup
java.lang.Object
????
android.view.View
?
????
android.view.ViewGroup
?
?
????
android.widget.RelativeLayout
java.lang.Object
????
android.view.View
?
????
android.view.ViewGroup
?
?
????
android.widget.RelativeLayout
void android.widget.RelativeLayout.LayoutParams.addRule(int verb,?int anchor)
public void addRule
(int verb, int anchor) 設(shè)置控件的相對位置
Added in API level 1
Adds a layout rule to be interpreted by the RelativeLayout. Use this for verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean value (VISIBLE).
ParametersOne of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.The id of another view to use as an anchor, or a boolean value(represented asTRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.
package?sunny.example.layoutparamstaddrule;
import?android.content.Context;
import?android.util.AttributeSet;
import?android.widget.Button;
import?android.widget.RelativeLayout;
import?android.widget.RelativeLayout.LayoutParams;
import?android.widget.TextView;
public??class?TestView?extends?RelativeLayout{
private?LayoutParams?mLayoutParams_1,mLayoutParams_2;
Button?mButton;
TextView?mTextView;
public?TestView(Context?context)?{
super(context);
//?TODO?Auto-generated?constructor?stub
mButton?=?new?Button(context);
mTextView?=?new?TextView(context);
init();
}
public?TestView(Context?context,?AttributeSet?attrs)?{
super(context,?attrs);
//?TODO?Auto-generated?constructor?stub
mButton?=?new?Button(context);
mTextView?=?new?TextView(context);
init();
}
public?TestView(Context?context,?AttributeSet?attrs,?int?defStyle)?{
super(context,?attrs,?defStyle);
//?TODO?Auto-generated?constructor?stub
mButton?=?new?Button(context);
mTextView?=?new?TextView(context);
init();
}
public?void?init(){
mLayoutParams_1?=?new?LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
mLayoutParams_1.addRule(RelativeLayout.ALIGN_TOP);?
addView(mButton,mLayoutParams_1);
mLayoutParams_2?=?new?LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
mLayoutParams_2.addRule(RelativeLayout.BELOW,?TRUE);
mTextView.setText("Hey");
addView(mTextView,mLayoutParams_2);
}
}Thanks to 泡在網(wǎng)上的日子
| verb | anchor |
|---|





