報出異常的原因是由于少添加了構造方法,三個構造方法需要寫完整,不能只寫一個。參數為(Context, AttributeSet),其中第二個參數用來將xml文件中的屬性初始化。
自定義控件若需要在xml文件中使用,就必須重寫帶如上兩個參數的構造方法。
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); } }
自定義控件在XML文件中使用
LayoutPrams
RelativeLayout.LayoutParams
extends ViewGroup.MarginLayoutParams
java.lang.Object
????
android.view.ViewGroup.LayoutParams
?
????
android.view.ViewGroup.MarginLayoutParams
?
?
????
android.widget.RelativeLayout.LayoutParamsRelativeLayout.LayoutParams(Context c,
AttributeSet attrs)
RelativeLayout.LayoutParams(int w, int h)
RelativeLayout.LayoutParams(ViewGroup.LayoutParams source)
RelativeLayout.LayoutParams(ViewGroup.MarginLayoutParams source)
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams source)Copy constructor.
RelativeLayout.LayoutParams的方法: public void addRule (int verb)
Adds a layout rule to be interpreted by the RelativeLayout. This method should only be used for constraints that don't refer to another sibling (e.g., CENTER_IN_PARENT) or take a boolean value (TRUE
for true or
0 for false). To specify a verb that takes a subject, use addRule(int, int)
instead.
ParametersOne of the verbs defined by RelativeLayout
, such as ALIGN_WITH_PARENT_LEFT.
public void addRule
(int verb, int anchor)
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
RelativeLayout.LayoutParams.addRule() 方法,該方法有兩種重載方式:
addRule(int verb) :用此方法時,所設置節(jié)點的屬性不能與其它兄弟節(jié)點相關聯或者屬性值為布爾值(布爾值的屬性,設置時表示該屬性為 true,不設置就默認為 false),比如:addRule(RelativeLayout.CENTER_VERTICAL)
就表示在 RelativeLayout 中的相應節(jié)點是垂直居中的。
addRule(int verb, int anchor) :該方法所設置節(jié)點的屬性必須關聯其它的兄弟節(jié)點或者屬性為布爾值( 屬性為布爾值時,anchor 為 RelativeLayout.TRUE
表示 true,anchor 為0表示 false),比如:addRule(RelativeLayout.ALIGN_LEFT, R.id.date) 就表示 RelativeLayout 中的相應節(jié)點放置在一個 id 值為 date 的兄弟節(jié)點的左邊。
Public Constructors | verb | verb | anchor |
---|