栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

详解Android自定义控件属性

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解Android自定义控件属性

在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义
控件时,难免要用到自定义属性,那怎么使用自定义属性呢?

在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性。




  
     
     
 

public class CustomTextView extends TextView {
  private int textSize;//自定义文件大小
  private int textColor;//自定义文字颜色

  //自定义属性,会调用带两个对数的构造方法
  public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray属性对象
    textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//获取属性对象中对应的属性值 
    textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
    setColorAndSize(textColor, textSize);//设置属性
    ta.recycle();
  }

  public CustomTextView(Context context) {
    super(context);
  }

  private void setColorAndSize(int textColor, int textSize) {
    setTextColor(textColor);
    setTextSize(textSize);
  }

}



  


布局说明:


通过以上几步就可以实现我们想要的自定义属性效果(用自定义属性设置文字大小及颜色)啦!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号