android怎么给控件添加属性

android怎么给控件添加属性,第1张

自定义属性设置

public class lei extends RelativeLayout {

private TextView tv1

public lei(Context context) {

super(context)

}

public lei(Context context, AttributeSet attrs) {

super(context, attrs)

LayoutInflater.from(context).inflate(R.layout.item,this)

tv1 = findViewById(R.id.tv1)

TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.lei)

String str1 = (String) array.getText(R.styleable.lei_settitle)

int str2 = array.getColor(R.styleable.lei_setbackgroudcolor,Color.BLACK)

tv1.setText(str1)

tv1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

Toast.makeText(getContext(), "toast", Toast.LENGTH_SHORT).show()

}

})

array.recycle()

}

}

布局控件

<com.example.administrator.myapplication.lei

android:id="@+id/ll"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

cs:setbackgroudcolor="#158616"

cs:settitle="这个是标题"

cs:settitletextcolor="#FFFFFF"

cs:settextrcolor="#FFFFFF"

>

</com.example.administrator.myapplication.lei>

属性设置

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

<resources>

<declare-styleable name="lei" >

<attr name="setbackgroudcolor" format="color|reference"/>

<attr name="settitle" format="string|reference"/>

<attr name="settextrcolor" format="color"/>

<attr name="settitletextcolor" format="color"/>

</declare-styleable>

</resources>

可以通过控件的ID.属性名的方式设置属性

例如我在窗体编辑页面中拖了一个TextBox,它的默认ID是TextBox1

那么如果我需要修改它的文本,也就是Text属性,则写TextBox1.Text=“需要修改的文本”

你单击那个控件右边就显示该控件的属性,你说的闹钟是计时器控件吧?要注意计时器控件属性的三要素enabled、interval、 timer事件设置好就可以了

添加属性是在工具栏”表单“里就有添加属性的选项。希望可以帮助到你


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11695244.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存