详解Android自定义控件属性

作者:BetterLaterThanNever 时间:2023-09-27 18:00:34 

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

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


<?xml version="1.0" encoding="utf-8"?>

<resources><!-- resource是跟标签,可以在里面定义若干个declare-styleable -->
 <declare-styleable name="custom_view"><!-- name定义了变量的名称 -->
   <attr name="custom_color" format="color"></attr> <!-- 定义对应的属性,name定义了属性的名称 -->
   <attr name="custom_size" format="dimension"></attr> <!--每一个发生要定义format指定其类型,类型包括
    reference  表示引用,参考某一资源ID
    string  表示字符串
    color  表示颜色值
    dimension  表示尺寸值
    boolean  表示布尔值
    integer  表示整型值
    float  表示浮点值
    fraction  表示百分数
    enum  表示枚举值
    flag  表示位运算
   -->
</declare-styleable>

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);
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#f6f6f6"
 android:orientation="vertical"
 android:padding="10dp" >

<com.ldm.learn.CustomTextView
   android:layout_width="100dp"
   android:layout_height="100dp"
   android:text="自定义TextView"
   ldm:custom_color="#333333"
   ldm:custom_size="35sp" />

</LinearLayout>

布局说明:

详解Android自定义控件属性

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

标签:Android,自定义控件
0
投稿

猜你喜欢

  • 详解Http请求中Content-Type讲解以及在Spring MVC中的应用

    2022-10-19 04:14:11
  • 利用 filter 机制给静态资源 url 加上时间戳,来防止js和css文件的缓存问题

    2022-03-16 07:51:24
  • springMvc注解之@ResponseBody和@RequestBody详解

    2022-10-09 17:57:19
  • C#8.0新语法using declaration

    2023-10-23 00:57:54
  • 总结C#删除字符串数组中空字符串的几种方法

    2022-04-14 03:26:42
  • Android中利用xml文件布局修改Helloworld程序

    2023-10-24 08:45:05
  • WPF调用ffmpeg实现屏幕录制

    2023-04-23 13:57:00
  • c#操作Redis的5种基本类型汇总

    2021-06-03 03:24:07
  • SpringBoot Java后端实现okhttp3超时设置的方法实例

    2022-11-06 04:56:03
  • java字符串遍历的几种常用方法总结

    2022-08-19 06:36:41
  • 设置Android系统永不锁屏永不休眠的方法

    2021-06-28 12:01:21
  • 基于Spring Boot不同的环境使用不同的配置方法

    2022-11-06 13:21:20
  • Android基于OpenCV实现图像修复

    2022-04-28 00:51:39
  • 基于Unity3D实现3D照片墙效果

    2023-05-22 00:18:51
  • 出现SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.的解决方法

    2021-09-01 12:02:09
  • Java编程实现从尾到头打印链表代码实例

    2021-12-28 12:13:41
  • android仿iphone主题效果的主菜单

    2023-04-29 03:56:35
  • Java栈和基础队列的实现详解

    2023-07-02 05:36:59
  • Java 批量删除Word中的空白段落示例代码

    2023-09-17 16:36:53
  • Java String类字符串的理解与认知

    2022-05-10 17:27:12
  • asp之家 软件编程 m.aspxhome.com