android Button源码分析

在Android中Button是一个非常常用的控件,下面我们就一起来分析一下Button源代码。

1 Button.java

 Button的源代码如下,非常简单几个构造器,它继承自TextView,添加了一个默认的com.android.internal.R.attr.buttonStyle样式。如果有时间可以学习一下TextView的源码

public class Button extends TextView {

    publicButton(Context context) {

        this(context, null);

    }

 

    publicButton(Context context, AttributeSet attrs) {

        this(context, attrs, com.android.internal.R.attr.buttonStyle);

    }

 

    publicButton(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }

}

2 styles.xml

在android源码的styles.xml文件中关于Button的样式:

<style name="Widget.Button">

        <item name="android:background">@android:drawable/btn_default</item>

        <item name="android:focusable">true</item>

        <item name="android:clickable">true</item>

        <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>

        <item name="android:textColor">@android:color/primary_text_light</item>

        <item name="android:gravity">center_vertical|center_horizontal</item>

</style>

 

<style name="Widget.Button.Small">

        <item name="android:background">@android:drawable/btn_default_small</item>

</style>

 

 <style name="Widget.Button.Inset">

        <item name="android:background">@android:drawable/button_inset</item>

</style>

 

<style name="Widget.Button.Transparent">

        <item name="android:background">@android:drawable/btn_default_transparent</item>

        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>

        <item name="android:textColor">@android:color/white</item>

</style>

这里我们能看到button各种获取焦点、被按下的各种样式是由btn_default、btn_default_small、button_inset、btn_default_transparent这几个xml文件来控制。

 

3 btn_default.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_window_focused="false" android:state_enabled="true"

        android:drawable="@drawable/btn_default_normal" />

    <item android:state_window_focused="false" android:state_enabled="false"

        android:drawable="@drawable/btn_default_normal_disable" />

    <item android:state_pressed="true"

        android:drawable="@drawable/btn_default_pressed" />

    <item android:state_focused="true" android:state_enabled="true"

        android:drawable="@drawable/btn_default_selected" />

    <item android:state_enabled="true"

        android:drawable="@drawable/btn_default_normal" />

    <item android:state_focused="true"

        android:drawable="@drawable/btn_default_normal_disable_focused" />

    <item

         android:drawable="@drawable/btn_default_normal_disable" />

</selector>

 定义了Button各种状态下用到的图片。所以如果我们想自定义一个Button的样式的话我们只需要仿着这个文件来写Button的样式就行了。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值