error inflating class binaryXML LayoutParams addRule()

报出异常的原因是由于少添加了构造方法,三个构造方法需要写完整,不能只写一个。参数为(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文件中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="sunny.example.layoutparamstaddrule.MainActivity" >
    
<sunny.example.layoutparamstaddrule.TestView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
</sunny.example.layoutparamstaddrule.TestView>

</RelativeLayout>

LayoutPrams

RelativeLayout.LayoutParams

extends ViewGroup.MarginLayoutParams
java.lang.Object
   ↳android.view.ViewGroup.LayoutParams
    ↳android.view.ViewGroup.MarginLayoutParams
     ↳android.widget.RelativeLayout.LayoutParams
Public Constructors
RelativeLayout.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.

Parameters
verbOne 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).

Parameters
verbOne of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.
anchorThe id of another view to use as an anchor, or a boolean value(represented as TRUE) 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 的兄弟节点的左边。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值