为自定View添加相应属性,在XML可以直接使用

本文介绍了如何在Android中为自定义View添加属性,以便在XML布局中直接使用。通过在values目录下创建declare-styleable资源文件,定义自定义属性,并在View的构造函数中通过TypedArray获取这些属性,然后绑定到相应的UI元素上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在values下新建一个atts文件夹,添加 
declare-styleable
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Topbar">
        <attr name="title" format="string"></attr>
        <attr name="titleTextSize" format="dimension"></attr>
        <attr name="titleTextColor" format="color"></attr>
        <attr name="leftTextColor" format="color"></attr>
        <attr name="leftBackground" format="color|reference"></attr>
        <attr name="leftText" format="string"></attr>
        <attr name="rightText" format="string"></attr>
        <attr name="rightBackground" format="color|reference"></attr>
        <attr name="rightTextColor" format="color"></attr>
    </declare-styleable>
</resources>

XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


  <com.example.hc.mytopbar.TopBar
      android:id="@+id/tp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      custom:leftText="back"
      custom:leftTextColor="#ffff00"
      custom:rightTextColor="#ffff00"
      custom:rightText="menu"
      custom:title="菜单"
      custom:titleTextColor="#123412"
      custom:titleTextSize="10sp">

  </com.example.hc.mytopbar.TopBar>

</RelativeLayout>

然后在自定义View类中的构造函数中 通过
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); 获得这个atts文件
获得atts中自定义属性之后 将属性与相应的控件绑定
public class TopBar extends RelativeLayout {
    private Button leftButton, rightButton;
    private TextView tv_title;

    private int leftTextColor;
    private Drawable leftBackground;
    private String leftText;
    private int rightTextColor;
    private Drawable rightBackground;
    private String rightText;

    private float titleTextSize;
    private int titleTextColor;
    private String title;

    private LayoutParams leftParams, rightParams, titleParam;

    private topbarClickListener listener;

    public interface topbarClickListener {
        public void leftClick();

        public void rightClick();
    }

    public void setOnTopbarClickListener(topbarClickListener listner) {
        this.listener = listner;
    }

    public TopBar(final Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftText = ta.getString(R.styleable.Topbar_leftText);

        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightText = ta.getString(R.styleable.Topbar_rightText);

        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
        title = ta.getString(R.styleable.Topbar_title);

        ta.recycle();
        setBackgroundColor(0xfff59563);
        leftButton = new Button(context);
        rightButton = new Button(context);
        tv_title = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setText(leftText);
        leftButton.setBackground(leftBackground);
        rightButton.setText(rightText);
        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightBackground);
        tv_title.setTextColor(titleTextColor);
        tv_title.setTextSize(titleTextSize);
        tv_title.setText(title);
        tv_title.setGravity(Gravity.CENTER);


        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(ALIGN_PARENT_LEFT, TRUE);

        addView(leftButton, leftParams);


        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(ALIGN_PARENT_RIGHT, TRUE);


        addView(rightButton, rightParams);

        titleParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParam.addRule(CENTER_IN_PARENT);
        addView(tv_title, titleParam);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值