Android中使用attrs.xml文件定制

1.在res/values下创建attrs.xml

<declare-styleable name="MyRadioButton">
        <attr name="str" format="string"/>
</declare-styleable>

MyRadioButton为组件名字,随意起,attr标签定义组件的属性,name对应的是属性名,format是属性的类型,具体可参见《 [Android]attrs.xml文件中属性类型format值的格式》。

2.在自定义的组件中使用attrs.xml文件的定义

 

public class MyRadioButton extends RadioButton {
    private String url;
     
    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray taArray = context.obtainStyledAttributes(attrs,R.styleable.MyRadioButton);
        this.url = taArray.getString(R.styleable.MyRadioButton_str);
        taArray.recycle();
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }     
 
}

 

a. TypedArray是存放资源R.styleable.MyRadioButton指定的属性集合。 
b. 通过getXXX()获取属性值。 
c. recycle()结束绑定 3.在布局文件中使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RadioGroup
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       >
        <net.csdn.blog.wxg630815.MyRadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/myradio1"
            demo:str="1.csdn.net"
            />
        <net.csdn.blog.wxg630815.MyRadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_parent"
            android:id="@+id/myradio2"
            demo:str="2.csdn.net"
            />
        
   </RadioGroup>
 
</LinearLayout>

注意: xmlns:demo="http://schemas.android.com/apk/res/net.csdn.blog.wxg630815"

只有声明这句以后,url属性才会被布局文件识别。net.csdn.blog.wxg630815指的是AndroidManifest.xml文件中manifest元素的package属性值。

使用demo:str给url赋值。

 

 

attrs.xml文件中属性类型format值的格式

"reference" //引用
"color" //颜色
"boolean" //布尔值
"dimension" //尺寸值
"float" //浮点值
"integer" //整型值
"string" //字符串
"fraction" //百分数,比如200%

枚举型的格式:

<attr name="orientation">
  <enum name="horizontal" value="0" />
  <enum name="vertical" value="1" />
</attr>

XML文件中使用:

android:orientation = "vertical" 

标志位、位或运算,格式如下:

<attr name="windowSoftInputMode">
  <flag name = "stateUnspecified" value = "0" />
  <flag name = "stateUnchanged" value = "1" />
  <flag name = "stateHidden" value = "2" />
  <flag name = "stateAlwaysHidden" value = "3" />
  <flag name = "stateVisible" value = "4" />
  <flag name = "stateAlwaysVisible" value = "5" />
  <flag name = "adjustUnspecified" value = "0x00" />
  <flag name = "adjustResize" value = "0x10" />
  <flag name = "adjustPan" value = "0x20" />
  <flag name = "adjustNothing" value = "0x30" />
</attr>

XML文件中使用:

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">  

属性定义可以指定多种类型:

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

ML文件中使用:

android:background = "@drawable/图片ID|#00FF00"  

 

 

转载于:https://www.cnblogs.com/amaker/p/3413787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值