使用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的一個(gè)內(nèi)部類(lèi), 每個(gè)不同的ViewGroup都有自己的LayoutParams子類(lèi)
DEMO:
//創(chuàng)建一個(gè)線性布局???? private?LinearLayout?mLayout;???? mLayout?=?(LinearLayout)?findViewById(R.id.layout);? //現(xiàn)在要往mLayout里邊添加一個(gè)TextView???? ???? TextView?textView?=?new?TextView(Activity01.this);???? textView.setText("Text?View?"?);???? //這里是設(shè)置?這個(gè)textView的布局?FILL_PARENT?WRAP_CONTENT?和在xml文件里邊設(shè)置是一樣的如???? /***/ ???? //第一個(gè)參數(shù)為寬的設(shè)置,第二個(gè)參數(shù)為高的設(shè)置。???? LinearLayout.LayoutParams?p?=?new?LinearLayout.LayoutParams(???? LinearLayout.LayoutParams.FILL_PARENT,???? LinearLayout.LayoutParams.WRAP_CONTENT???? );???? //調(diào)用addView()方法增加一個(gè)TextView到線性布局中???? mLayout.addView(textView,?p);???? //比較簡(jiǎn)單的一個(gè)例子
LayoutParams繼承于Android.View.ViewGroup.LayoutParams.
LayoutParams相當(dāng)于一個(gè)Layout的信息包,它封裝了Layout的位置、高、寬等信息。假設(shè)在屏幕上一塊區(qū)域是由一個(gè)Layout占領(lǐng)的,如果將一個(gè)View添加到一個(gè)Layout中,最好告訴Layout用戶(hù)期望的布局方式,也就是將一個(gè)認(rèn)可的layoutParams傳遞進(jìn)去。
可以這樣去形容LayoutParams,在象棋的棋盤(pán)上,每個(gè)棋子都占據(jù)一個(gè)位置,也就是每個(gè)棋子都有一個(gè)位置的信息,如這個(gè)棋子在4行4列,這里的“4行4列”就是棋子的LayoutParams。
但LayoutParams類(lèi)也只是簡(jiǎn)單的描述了寬高,寬和高都可以設(shè)置成三種值:
1,一個(gè)確定的值;
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è)置控件的相對(duì)位置
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 |
---|