Android自定义属性的基本使用

有些应用场景需要我们给现有的控件增加一些属性,优雅的实现预期效果,本文简单描述自定义属性的基本使用方法。

例子:给TextView增加一个 “选中” 和 “未选中” 显示不同的背景的属性

一、写属性

  1. 在values下新建一个名为attrs的xml文件。
    在这里插入图片描述
    2.在attrs.xml中定义自己的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="img_attr">
        <attr name="before" format="reference"/><!--未选中状态-->
        <attr name="after" format="reference"/><!--选中状态-->
    </declare-styleable>
</resources>

二、拿到属性实现功能

直接上代码:用代码说

package com.myApp.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.android.launcher3.R;


@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {

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

    public ColorViewL2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected int id_n = 0;
    protected int id_p = 0;
    public ColorViewL2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //1. 获取属性(本教程的关键所在)
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.img_attr);
        id_n = typedArray.getResourceId(R.styleable.img_attr_before, 0);//R.styleable.父名_子名
        id_p = typedArray.getResourceId(R.styleable.img_attr_after, 0);//R.styleable.父名_子名
        setSelect(context,false);
    }

    
    //2。复写TextView设置背景的方法
    @Override
    public void setBackground(Drawable background) {
        super.setBackground(background);
    }

    //3. 对外public这个View动态设置背景
    public void setSelect(Context context,boolean isSelect){
        if (isSelect){
            if (id_p!=0)setBackground(ContextCompat.getDrawable(context,id_p));
        }else {
            if (id_n!=0)setBackground(ContextCompat.getDrawable(context,id_n));
        }
    }

}

三、使用方法

1. 在你的布局头部加上这样一句代码:xmlns:myattrs=“http://schemas.android.com/apk/res-auto”

2.在你的布局文件中就可以使用myattrs拿到你的定义的属性


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:myattrs="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/__l2__ms_pedal_switch_bg">

<com.myApp.view.MyTextView
                    android:id="@+id/colorView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    myattrs:before="@drawable/img_1"
                    myattrs:after="@drawable/img_2" />
                    
<com.myApp.view.MyTextView
                    android:id="@+id/colorView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    myattrs:before="@drawable/img_3"
                    myattrs:after="@drawable/img_4" />
        
</RelativeLayout>

四、最终效果

MyTextView ms=(MyTextView)findViewById(R.id.colorView1);

//选中
ms.setSelect(context,true);

//不选中
ms.setSelect(context,false);

不需要你写很多setBackgrund的逻辑

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 自定义按钮可以通过自定义样式和布局文件来实现。下面是一个简单的步骤: 1. 创建一个新的 XML 布局文件,例如 custom_button.xml。 2. 在布局文件中定义按钮的外观和样式。你可以设置按钮的背景、文字颜色、边框等。 3. 在你的 Activity 或 Fragment 中使用自定义按钮。在布局文件中,使用`<com.example.CustomButton>`作为按钮的标签,其中`com.example`是你自定义按钮类的包名。 4. 在你的自定义按钮类中,继承 `Button` 类,并实现构造方法和一些自定义方法。你可以添加一些特定的行为或属性,如点击事件或动画效果。 5. 在自定义按钮类中,重写 `onDraw` 方法来绘制自定义的外观。你可以使用 `Canvas` 对象来绘制形状、颜色和文本。 6. 将自定义按钮类与布局文件中的按钮关联起来。在布局文件中使用 `app:buttonStyle="@style/CustomButtonStyle"` 来指定按钮的样式。 7. 在你的 styles.xml 文件中定义自定义按钮的样式,例如: ```xml <style name="CustomButtonStyle" parent="Widget.AppCompat.Button"> <item name="android:background">@drawable/custom_button_background</item> <item name="android:textColor">@color/custom_button_text_color</item> <!-- 其他样式属性 --> </style> ``` 8. 创建一个自定义背景资源文件 custom_button_background.xml,用于定义按钮的背景形状和颜色。 9. 运行你的应用程序,查看自定义按钮的效果。 这些是实现 Android 自定义按钮的基本步骤。你可以根据自己的需求和创意来进一步扩展和定制按钮的外观和行为。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝命三郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值