当前位置:首页 > 芯闻号 > 充电吧
[导读]报出异常的原因是由于少添加了构造方法,三个构造方法需要写完整,不能只写一个。参数为(Context, AttributeSet),其中第二个参数用来将xml文件中的属性初始化。自定义控件若需要在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 Constructorsverbverbanchor
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

今天我们来放松下心情,不聊分布式,云原生,来聊一聊初学者接触的最多的 java web 基础。

关键字: Spring xml java

引言   IEC 61850 是国际电工委员会负责电力系统控制及其通信的相关标准的第57 技术委员会( IEC TC57)制定的关于变电站自动化系统结构和数据通信的一个国际标准, 目的是

关键字: xml iec 61850标准

XML 之父 Tim Bray 近日发布了一篇名为《Why Google Did Android》的博客,根据他早些年亲自了解的情况,分享了谷歌当年为什么要开发 Android的原因。

关键字: Android xml 谷歌

先上代码 //从config.xml中获取标签appenvironment和oa_app_version的值 //appenvironment值为pro表示生产,为pre表示预生产 QMap rea

关键字: QT xml

Linux下libxml2编程基础 一、libxml2的简介     xml是一个实现读、创建、以及操作xml数据功能的C语言库。并且支持XPATH查询,以及部分的支持XSLT转换等的功能。   l

关键字: xml xpath

我们知道对XML的操作有两种方法,即DOM方式和SAX方式。二者主要区别是:DOM实现方式操作非常简单,但不适合处理过大文件;而SAX实现方式是能处理很大的XML文件,但是需要开发者写一些复杂的代码。

关键字: dom xml

XML特殊字符和空格 换行 转义符 标签: xmlwhitespacestringtextbox 2012-06-25 15:37 38006人阅读 评论(0) 收藏 举报 本文章已收录

关键字: xml

PBDOM操作XML文档轻松入门(Easy XML with PowerBuilder Document Object Model)(作者:Terry Voth、JohnStrano)  本文对PBD

关键字: pbdom xml

XML error parsing SOAP payload on line 2: Invalid document end 学习PHP的时候遇到这样的错误,百度都没有结果。自己测试了好久,终于找到了

关键字: xml
关闭
关闭