当前位置: 首页 > news >正文

慈善会网站建设方案舆情系统

慈善会网站建设方案,舆情系统,wordpress没中文插件,公众号发票怎么开一、概念 在 xml 中为控件设置的属性。自定义属性名称如果使用系统已定义的&#xff0c;例如 textSize 会在编译时报错。 格式类型定义/使用 string 字符串 <attr name "myContent" format "color" /> android:myContent "Hello Word!&quo…

一、概念 

在 xml 中为控件设置的属性。自定义属性名称如果使用系统已定义的,例如 textSize 会在编译时报错。

格式类型定义/使用

string 字符串

<attr name = "myContent" format = "color" />

android:myContent = "Hello Word!"

color 颜色

<attr name = "myTextColor" format = "color" />

android:myTextColor = "#00FF00"

dimension 尺寸

<attr name = "myTextSize" format = "dimension" />

android:myTextSize = "12.sp"

reference 资源

<attr name = "myBackground" format = "reference" />

android:myBackground = "@drawable/图片ID"

boolean 布尔

<attr name = "myEnable" format = "boolean" />

android:myEnable = "true"

float 浮点

<attr name = "myAlpha" format = "float" />

android:myAlpha = "0.5F"

integer 整型

<attr name = "myMaxLines" format = "integer" />

android:myMaxLines = "3"

fraction 百分比

<attr name = "myOffset" format = "fraction" />

android:myOffset = "10%"

enum 枚举

<attr name = "myOrientation">

        <enum name = "horizontal" value="0" />

        <enum name = "vertical" value="1" />

</attr>

android:myOrientation = "vertical"

flag 位运算

位运算类型的属性在使用的过程中可以使用多个值

<attr name = "myGravity" />

        <flag nema="top" value="0x01">

        <flag nema="left" value="0x02">

        <flag nema="center_vertical" value="0x02">

</attr>

android:myGravity = "top|left"

混合类型

属性定义时可以指定多种类型值

<attr name = "myBackground" format = "reference|color" />

android:myBackground = "@drawable/图片ID"

android:myBackground = "#00FF00"

二、自定义步骤

2.1 创建资源文件(属性声明)

右键 values 目录 -> New File文件 -> 一般取名attrs.xml。

<resources><!--name使用自定义View的名称--><declare-styleable name="MyView"><!--name属性名称,format格式--><attr name="myText" format="string" /><attr name="myTextColor" format="color" /><attr name="myTextSize" format="dimension" /><attr name="myMaxLength" format="integer" /><attr name="myBackground" format="reference|color" /><!--枚举--><attr name="myInputType"><enum name="number" value="1"/><enum name="text" value="2"/></attr></declare-styleable>
</resources>

2.2 构造函数中配置

constructor(context: Context?) : super(context)

重写一个参数的构造函数,使用场景:代码 new 创建实例的时候调用。

constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

重写两个参数的构造函数,使用场景:xml中使用时调用(xml转java代码的时候反射)。

constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

重写三个参数的构造函数,使用场景:使用主题Style的时候调用。

class MyView : View {private var text: String? = nullprivate var textSize: Int? = nullprivate var textColor: Int? = nullprivate var maxLength: Int? = nullprivate var background: Int? = nullprivate var inputType: Int? = null//改成this调用2个参数的构造constructor(context: Context?) : this(context, null)//改成this调用3个参数的构造constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)//在这里统一进行处理constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {context?.let {//返回一个与attrs中列举出的属性相关的数组,数组里面的值由样式属性指定val attributes = it.obtainStyledAttributes(attrs, R.styleable.MyView)//获取自定义属性(格式:属性名称_条目名称)text = attributes.getString(R.styleable.MyView_myText)textSize = attributes.getDimensionPixelSize(R.styleable.MyView_myTextSize, 0)textColor = attributes.getColor(R.styleable.MyView_myTextColor, Color.BLACK)maxLength = attributes.getInt(R.styleable.MyView_myMaxLength,1)background = attributes.getResourceId(R.styleable.MyView_myBackground,R.drawable.ic_launcher_foreground)inputType = attributes.getInt(R.styleable.MyView_myInputType,0)//回收资源attributes.recycle()}}
}

2.3 布局中使用(属性使用)

  • 根布局添加命名空间(只需要输入app,IDE会自动补全)。
  • 控件名称使用完整路径(只需要输入自定义View的类名,IDE会自动补全)。
  • 未自定义的属性View会去处理(继承自View),使用自定义的属性就是 app: 开头的。
<!--根布局添加命名空间-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--控件名称使用完整路径--><com.example.kotlindemo.view.MyViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:myBackground="@drawable/ic_launcher_foreground"app:myInputType="number"app:myText="Hello Word!"app:myTextColor="@color/black"app:myTextSize="20sp"app:myMaxLength="20"/>
</LinearLayout>
http://www.hrbkazy.com/news/38149.html

相关文章:

  • 临沂网站建设推荐怎么做好网站方式推广
  • 好看的扁平化网站引擎搜索优化
  • 哪些产品可以做单页网站怎样才能上百度
  • 重庆网站制作的网站网站seo诊断优化方案
  • 建立可以在线做照片的网站市场营销手段有哪四种
  • 猪八戒做网站怎么样推广图片大全
  • 做一件代发的网站外链查询工具
  • 天津做网站哪家好友情链接检测
  • 用dw做动态网站乱码怎么弄百度网页提交入口
  • html5标准网站建设重庆seo网站运营
  • 深圳龙华医院网站建设温州网站优化推广方案
  • 深圳网站建设公司哪家好网站建设情况
  • ps 做ui比较好的网站有哪些短链接在线生成器
  • 教育网站安全建设方案谷歌外链代发
  • 盗版小说网站怎么赚钱网络营销的特点包括
  • 一起做网站17怎么下单深圳优化公司义高粱seo
  • wordpress如何恢复百度seo查询工具
  • 网站开发做前端还是后端app软件开发制作公司
  • 服务器可以吧网站做跳转吗高中同步测控优化设计答案
  • 云南建站网络营销第三版课本
  • 深圳百度seo代理泰州百度seo
  • 做网站学什么什么专业武汉网站推广优化
  • 小组做数据库网站青岛网络推广公司
  • 仿网站百度会怎么做重庆seo杨洋
  • 公司做网站的价格几千元b站2020推广网站
  • 做ppt选小图案的网站泰州网站排名seo
  • 做网站需要什么素材怎么做公众号
  • 如何建导航网站宁波网站seo诊断工具
  • 自己电脑怎么做网站服务器深圳专业建站公司
  • 宿迁做网站公司种子搜索引擎在线