Android UI控件之CheckBox实现墨迹天气设置布局界面(漂亮)
2013-03-17 19:55
711 查看
Android中的控件的自定义性是很强的,正因为如此我们可以更容易的做出漂亮的UI。下面就
是通过自定义CheckBox实现墨迹天气,qq等等的设置界面。
话不多说,直接上效果图吧!
效果还算可以吧?下面来看看具体的实现吧!
主xml文件:
可以将重复的代码添加到style.xml的文件中去设置为一种样式,在需要这种样式的时候在使用!
下面是该小项目的样式文件。
style文件:
首先看复选框的实现:
值得注意的是上面中第一项和第三项的边框都是圆形的,而中间是方的,这只是设置的
背景图片,这里就不深究了,好了Over!
完整源码:
是通过自定义CheckBox实现墨迹天气,qq等等的设置界面。
话不多说,直接上效果图吧!
效果还算可以吧?下面来看看具体的实现吧!
主xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DFE1E0" android:orientation="vertical"> <LinearLayout style="@style/SettingItemTop" android:background="@drawable/setting_list_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView style="@style/MySettingText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启通知" /> <CheckBox style="@style/MyCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout style="@style/SettingItemMiddle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView style="@style/MySettingText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启铃声" /> <CheckBox style="@style/MyCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout style="@style/SettingItemBottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView style="@style/MySettingText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启震动" /> <CheckBox style="@style/MyCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>注意到:
style="@style/SettingItemBottom"使用了style.xml这个文件来控制样式。在Android开发中会有很多控件的属性是重复出现的,
可以将重复的代码添加到style.xml的文件中去设置为一种样式,在需要这种样式的时候在使用!
下面是该小项目的样式文件。
style文件:
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="AppTheme" parent="android:Theme.Light" /> <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/check</item> <item name="android:layout_margin">10dp</item> <item name="android:layout_weight">1</item> </style> <style name="MySettingText"> <item name="android:layout_margin">10dp</item> <item name="android:layout_weight">6</item> </style> <style name="SettingItemTop"> <item name="android:layout_marginTop">10dp</item> <item name="android:layout_marginLeft">10dp</item> <item name="android:layout_marginRight">10dp</item> <item name="android:background">@drawable/setting_list_top</item> </style> <style name="SettingItemMiddle"> <item name="android:layout_marginLeft">10dp</item> <item name="android:layout_marginRight">10dp</item> <item name="android:background">@drawable/setting_list_middle</item> </style> <style name="SettingItemBottom"> <item name="android:layout_marginBottom">10dp</item> <item name="android:layout_marginLeft">10dp</item> <item name="android:layout_marginRight">10dp</item> <item name="android:background">@drawable/setting_list_bottom</item> </style> </resources>上述效果的主要代码也就是这些了,具体是如何实现的呢?
首先看复选框的实现:
<CheckBox style="@style/MyCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content"/>抓出他的样式:可以的出的checkBox的样式使用了selector
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/check</item> <item name="android:layout_margin">10dp</item> <item name="android:layout_weight">1</item> </style>也就是;
<item name="android:button">@drawable/check</item>在资源文件res/drawable建立一个名为check的xml文件,内容为:
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/on"/> <item android:state_checked="false" android:drawable="@drawable/off"/> </selector>这样就定义了,checkBox选中和没选中所显示的图片按钮,checkBox部分也就完成了!
值得注意的是上面中第一项和第三项的边框都是圆形的,而中间是方的,这只是设置的
背景图片,这里就不深究了,好了Over!
完整源码:
相关文章推荐
- Android UI控件之CheckBox实现墨迹天气设置布局界面(漂亮)
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- Android fragment 横竖屏的动态布局实现(不需要保存数据)动态的给控件设置权重
- android实现从系统图库中选择图片,设置到界面上的ImageView控件上。
- Lance老师UI系列教程第一课->QQ设置界面的实现(android)
- Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现
- android下一个典型的设置界面的布局实现 .
- Lance老师UI系列教程第一课->QQ设置界面的实现(android)
- (android控件)界面布局实现GridView(网格效果)
- 深入浅出android/ophone UI实现水平布局的Tab控件
- Lance老师UI系列教程第一课->QQ设置界面的实现(android)
- Android UI,界面辅助设置工具,可随意拖动控件,比google官方提供的方便
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- 深入浅出android/ophone UI实现水平布局的Tab控件 Tabwidget在左边/右边
- 在Android的UI开发中,有时为了界面美观而需要使用虚线。在这里我自己实现一个画虚线的控件。
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- 用Android布局和简单控件实现的一个常见的界面