Android自定义View之不得不知道的文件attrs.xml(自定义属性)

简介: 本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。

系列文章目录

Android自定义View之不得不知道的文件attrs.xml(自定义属性)


先介绍一下Demoview:第一张图红框内是后续的代码

布局文件demoview.xml如下;里面放了一个Textview和ImageView。

attrs.xml文件

这个时候如果在Activity中运用的话,就是一个死的view,那么有没有什么办法,能改变里面的东西呢(改变文字&&设置ImageView是否显示)
这里就不得不提到文章标题中的attrs.xml文件了:如下图

写好后是当然是运用了(其中clickable="true"是设置该控件可以点击,不设置点击变色的背景无效),图片中打错个字,,,,懒得改了

其中demo_style如下:一般都是放的.9图片,为了方便就放个颜色了

最后当然是要将自定义的属性实现其功能了(回到第一步中)

这两句就是取得你在运用中输入的东西了
其中:为了使title相当于TextView中的text:mtv.setText(),将获取的值放入就行了。

那么,那个boolean的值是用来干嘛的呢:控制图片是否显示--> miv.setVisibility(b ? VISIBLE : GONE);如果b为true则显示,为false则不显示。(拓展:了解一下VISIBLE :,GONE,INVISIBLE);

好了现在已经完成了这两个功能了,现在来提一下,点击更换图片

最后在Activity中调用:

DemoView.java代码如下:

public class DeomView extends RelativeLayout {
    private ImageView miv;
    private TextView mtv;
    private boolean yfisshow;

    public DeomView(Context context) {
        this(context, null);
    }

    public DeomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public DeomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        String title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "title");
        boolean b = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "isshow", true);
        View view = View.inflate(context, R.layout.demoview, null);
        addView(view);
        mtv= (TextView) view.findViewById(R.id.tv_togle);
        miv = (ImageView) view.findViewById(R.id.iv_togle);
        miv.setVisibility(b ? VISIBLE : GONE);
        mtv.setText(title);

    }

    //改变状态
    public void setSetting0n(boolean isshow) {
        yfisshow = isshow;
        miv.setImageResource(isshow ? R.mipmap.on : R.mipmap.off);
    }
    //记录状态
    public boolean issetting() {
        return yfisshow;
    }
    public void setbackeguang() {
        //第一次优化
//        if (issetting()){
//            setSetting0n(false);
//        }else{
//           setSetting0n(true);
//        }
        //第二次优化
//          if (yfisshow){
//              setSetting0n(false);
//          }else{
//              setSetting0n(true);
//          }
        //第三次优化
//        if (yfisshow){
//            setSetting0n(!yfisshow);
//        }else{
//            setSetting0n(!yfisshow);
//        }
        setSetting0n(!yfisshow);
    }
}
相关文章
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
1005 1
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
Android开发
AutoX——当Android中clickable属性显示为false,实际可点击的布局如何处理
AutoX——当Android中clickable属性显示为false,实际可点击的布局如何处理
280 0
|
XML 开发工具 数据格式
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
255 0
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
|
缓存 安全 Java
Android中的persistent属性
Android中的persistent属性
1130 2
|
Android开发
Android android:exported="true" 属性
Android android:exported="true" 属性
2248 0
|
Java Android开发
Android开发--Intent-filter属性详解
Android开发--Intent-filter属性详解
419 0
|
Android开发 C++
Android 系统属性(SystemProperties)
Android 系统属性(SystemProperties)
1031 1
|
Java Maven
maven项目的pom.xml文件常用标签使用介绍
第四届人文,智慧教育与服务管理国际学术会议(HWESM 2025) 2025 4th International Conference on Humanities, Wisdom Education and Service Management
1231 8

热门文章

最新文章