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 stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubmButton = new Button(context);mTextView = new TextView(context);init();}public TestView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubmButton = 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.TestViewandroid: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 的兄弟节点的左边。


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348782.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AppD方法:Java 9支持

通过从应用程序中学习企业APM产品&#xff0c;发现更快&#xff0c;更高效的性能监控。 参加AppDynamics APM导览&#xff01; 阅读有关Java 9模块化功能带来的挑战以及AppDynamics保持在该领域的领导者的严格要求的更多信息。 我们很高兴宣布Java 17全面支持&#xff0c;这是…

Java反射,但速度更快

在编译时不知道Java类的最快方法是什么&#xff1f; Java框架通常会这样做。 很多。 它可以直接影响其性能。 因此&#xff0c;让我们对不同的方法进行基准测试&#xff0c;例如反射&#xff0c;方法句柄和代码生成。 用例 假设我们有一个简单的Person类&#xff0c;其中包含名…

Git时间

1、Git是目前世界上最先进的分布式版本控制系统。和集中式版本控制系统相比&#xff0c;分布式版本控制系统的安全性要高很多&#xff0c;因为每个人电脑里都有完整的版本库&#xff0c;某一个人的电脑坏掉了不要紧&#xff0c;随便从其他人那里复制一个就可以了。而集中式版本…

横、竖分割线

竖分割线&#xff1a; <View android:layout_width"0.5px" android:layout_height"120dp" android:background"#B8B8B8" android:visibility"visible" /> <!-- 竖直线 -->横分割线&#xff1a;<Viewandroid:layout…

java中update_Java 7 Update 21安全改进的详细信息

java中updateOracle昨天发布了三个Java更新 。 重要的是要注意它们包含一些与安全性相关的更改。 一段时间以来&#xff0c;这些变更中的大多数已经宣布&#xff0c;首先要注意的是Oracle按计划交付。 Oracle公司Java平台安全经理米尔顿史密斯&#xff08;Milton Smith&…

访问GitHub超慢的解决办法

是github某个CDN(Content Delivery Network&#xff0c;即内容分发网络?)被屏蔽所致。 附件--->记事本&#xff08;选择以管理员身份运行&#xff09;——文件——打开C:\Windows\System32\drivers\etc 右下角选择“所有文件” 选择hosts 打开 如图在这行下面添加绑定IP…

学习使用Whally GraalVM!

介绍 在Truffle在神圣的Graal中服务&#xff1a;Graal和Truffle在JVM上进行多语种语言解释的帖子中&#xff0c;我们得到了简短的介绍&#xff0c;并对Graal&#xff0c;Truffle及其周围的一些概念进行了深入研究。 但是&#xff0c;如果不深入研究实用性&#xff0c;那么任何技…

colos.xml

<?xml version"1.0" encoding"utf-8"?> <!-- /* //device/apps/common/assets/res/any/colors.xml ** ** Copyright 2006, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License");…

Android样式开发---shape

Thanks to:转载自Keegan小钢 原文链接&#xff1a;http://keeganlee.me/post/android/20150830 一个应用&#xff0c;应该保持一套统一的样式&#xff0c;包括Button、EditText、ProgressBar、Toast、Checkbox等各种控件的样式&#xff0c;还包括控件间隔、文字大小和颜色、阴影…

主键能否@onetoone_双向@OneToOne主键关联

主键能否onetoone现在该继续有关Hibernate的文章了。 最后一个致力于单向OneToOne关联 。 因此&#xff0c;今天我将向您展示如何获取双向OneTonOne主键关联 。 本教程中基于前一篇文章的示例。 让我们开始吧。 我将使用以前创建的相同表。 为了建立双向一对一关联&#xff0c…

Android样式开发--selector

Thanks to 转载自Keegan小钢 原文链接&#xff1a;http://keeganlee.me/post/android/20150905 上一篇详细讲了shape的用法&#xff0c;讲解了怎么用shape自定义矩形、圆形、线形和环形&#xff0c;以及有哪些需要注意的地方。不过&#xff0c;shape只能定义单一的形状&#xf…

Amazon Glacier的Scala客户端

Amazon Glacier是一项安全&#xff0c;耐用且成本极低的云存储服务&#xff0c;用于数据归档和长期备份。 Glacier提供了一种冷藏数据存档解决方案&#xff0c;这意味着已存储的数据不可立即检索。 您首先需要请求检索数据&#xff0c;访问时间可能从几分钟到几小时不等&#x…

Drawable Resources

https://developer.android.com/guide/topics/resources/drawable-resource.html#Transition

SwipeRefreshLayout官方推荐下拉刷新

SwipeRefreshLayoutpublic class SwipeRefreshLayout extends ViewGroup implements NestedScrollingParent, NestedScrollingChildjava.lang.Object↳android.view.View↳android.view.ViewGroup↳ android.support.v4.widget.SwipeRefreshLayout API doc&#xff1a;http://…

jboss加载组件_直接从JBoss AS 7组件使用JGroups

jboss加载组件JGroups是Bela Ban的用于可靠消息交换的软件&#xff0c;该软件高度可配置&#xff0c;可以使用TCP或UDP作为传输协议。 基本上&#xff0c;您在多个客户端上运行JGroup&#xff0c;它们构成一个集群&#xff0c;它们可以在集群内发送和接收消息。 JGroups由JBos…

开源PagerSlidingTabStrip的使用Tab与ViewPager的完美结合

链接地址&#xff1a;https://github.com/astuetz/PagerSlidingTabStrip 下载PagerSlidingTabStrip-master 将com.astuetz包&#xff0c;res下的下的资源复制进工程 布局文件&#xff1a;activity_main.xml <RelativeLayout xmlns:android"http://schemas.android.co…

如何处理异常

最近&#xff0c;我与一个朋友进行了讨论&#xff0c;他是一个相对初级但很聪明的软件开发人员。 她问我有关异常处理的问题。 这些问题指出了一种技巧和窍门&#xff0c;肯定有它们的清单。 但是我坚信我们编写软件的方式背后的背景和动机&#xff0c;因此我决定从这种角度写关…

android动画详解

转自&#xff1a;工匠若水 http://blog.csdn.net/yanbober 1 背景 不能只分析源码呀&#xff0c;分析的同时也要整理归纳基础知识&#xff0c;刚好有人微博私信让全面说说Android的动画&#xff0c;所以今天来一发Android应用的各种Animation大集合。英文厉害的请直接移步参考…

凹数科技笔试

一、Java 1、成员变量作用域public/protected/defaultprivate/区别&#xff1f; public&#xff1a;该成员变量或其方法对当前类、同一包、子类、其他包都可见&#xff0c;所有类和对象都可以直接访问。 protected&#xff1a;该成员变量或其方法对当前类、同一包、子类都可…

jsr250-api_JSON处理的Java API(JSR-353)–流API

jsr250-apiJava很快将具有一组标准的API&#xff0c;作为Java EE 7的一部分处理JSON。该标准定义为JSR 353 – JSON处理的Java API &#xff08;JSON-P&#xff09;&#xff0c;目前正在最终批准投票中。 JSON-P提供面向对象和基于流的方法&#xff0c;在本文中&#xff0c;我将…