如何延长一个Android按钮,并使用XML布局文件 [英] How to extend an Android button and use an xml layout file

查看:117
本文介绍了如何延长一个Android按钮,并使用XML布局文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扩大Android的按钮类,并将它使用XML布局文件。

我想用一个xml布局文件的原因是,我的按钮需要使用一种风格而据我所知,没有设置编程风格的一种方式。


  

公共类BuyButton扩展了Button {...}


 <?XML版本=1.0编码=UTF-8&GT?;
<按钮
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    风格=@风格/的CustomButton
/>

这样我就可以拨打电话:

 新BuyButton(活动);

和有它创建一个具有适用于它的风格的按钮。

(我也开到得到同样结果的其他方式)


解决方案

创建扩展一个类按钮

 公共类BuyButton扩展了Button {    公共BuyButton(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        // TODO自动生成构造函数存根
    }}

在你的XML引用,直接自定义类。

 <?XML版本=1.0编码=UTF-8&GT?;
< your.package.name.BuyButton
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
风格=@风格/的CustomButton/>

I'm trying to extend the android button class and have it use an xml layout file.

The reason I want to use an xml layout file is that my button needs to use a style and as far as I know, there isn't a way to set style programatically.

public class BuyButton extends Button { ... }

<?xml version="1.0" encoding="utf-8"?>  
<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/customButton"
/>

so that I can call:

new BuyButton(activity);

and have it create a button that has the style applied to it.

(I'm also open to other ways of getting the same result)

解决方案

Create a class that extends Button.

public class BuyButton extends Button {

    public BuyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

}

In your XML reference that custom class directly.

<?xml version="1.0" encoding="utf-8"?>  
<your.package.name.BuyButton 
xmlns:android="http://schemas.android.com/apk/res/android" 
style="@style/customButton"/>

这篇关于如何延长一个Android按钮,并使用XML布局文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆