android Button源码分析
2016-01-06 23:10
417 查看
在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的样式就行了。
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的样式就行了。
相关文章推荐
- Android ListView嵌套Button,Button事件覆盖item事件解决办法
- android骚年的年终总结和规划
- Android ProgressBar 样式【大全】
- android 连按两次退出程序
- 在android 中导入项目后 包出现错误
- Android输入法界面管理(打开/关闭/状态获取)
- Android 布局分析
- Android 通过串口模拟 模拟物理按键
- Android的SharedPreferences存储
- android camera:输出数据流和裁剪
- android动画的ZAdjustment
- Android 通过Intent调用系统功能和Action动作和服务广播【大全】
- [ApiDemos] Activity CustomDialog
- android开源项目总结
- android 传感器编程
- Android关于setExitTransition() 没有效果的问题
- Android之应用坐标系统全面详解
- android studio快捷键大全
- 获取arrays.xml中的图片以及颜色值
- 无线漫游原理