自定义控件在XML文件中使用异常处理
扫描二维码
随时随地手机看文章
报出异常的原因是由于少添加了构造方法,三个构造方法需要写完整,不能只写一个。参数为(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) :用此方法时,所设置节点的属性不能与其它兄弟节点相关联或者属性值为布尔值(布尔值的属性,设置时表示该属性为 true,不设置就默认为 false),比如:addRule(RelativeLayout.CENTER_VERTICAL)
就表示在 RelativeLayout 中的相应节点是垂直居中的。
addRule(int verb, int anchor) :该方法所设置节点的属性必须关联其它的兄弟节点或者属性为布尔值( 属性为布尔值时,anchor 为 RelativeLayout.TRUE
表示 true,anchor 为0表示 false),比如:addRule(RelativeLayout.ALIGN_LEFT, R.id.date) 就表示 RelativeLayout 中的相应节点放置在一个 id 值为 date 的兄弟节点的左边。
Public Constructors | verb | verb | anchor |
---|