android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
来源:互联网 发布:黑马程序员全套视频 编辑:程序博客网 时间:2024/06/05 08:35
android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
最终实现效果如下图:
具体来说就是实现了checkbox自定义选中和为选择样式,菜单项根据不同位置设置不同背景.
先上整体布局文件代码:
<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" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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/MySettingText"这一句,这里用了style.xml这个文件来控制样式.Android实际开发中,会有很多时候一些部件的属性是要重复出现的,如果每个控件都要单独的把这些属性单独的输入一次,那样会很没有效率.可以将重复的代码添加到style.xml中,设置成一种样式,在用到这些属性是引用这个样式即可.
style="@style/MySettingText"这个是设置文字的属性,代码如下:
<style name="MySettingText"> <item name="android:layout_margin">10dp</item> <item name="android:layout_weight">6</item> </style>
这里仅用了两个属性.开发中可以根据实际需要自行修改.以后每个设置项的文字不需要再把这些属性再添加一边,引用一下这个style即可.
CheckBox样式自定义用到了selector.
style="@style/MyCheckBox"此句对应的代码:
<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>
这里就用了selector自定义样式.在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>
其中
@drawable/on为选择时checkbox的图像,
@drawable/off反之.还注意到菜单中第一项上方为圆角,中间项为四角都为方,最后一项为下部圆角,这个就是通过设置不同的背景图片实现的了,具体不细表.
0 0
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- android checkbox设置自定义的样式
- 使用selector设置按钮不同状态的样式(Android)
- android 样式style的设置
- android 自定义checkBox的样式
- Android自定义样式的Checkbox
- Android style样式文件的使用
- WPF style 样式的使用
- style样式的简单使用
- Android学习篇章16-Style控件样式的使用
- 自定义样式的checkBox
- 自定义checkbox的样式
- 使用style样式隐藏显示下级菜单的方法
- CheckBox使用自己的样式
- Android selector自定义shape的button样式
- AN button自定义样式 && AN selector和shape的使用
- android自定义样式style的用法
- Android布局TextView文字设置不同样式Spannable的使用
- ubuntu13.10系统 安装中文输入法fcitx
- RSA加密算法在iOS9下的问题解决方案
- DL:RBM学习算法——Gibbs采样、变分方法、对比散度、模拟退火
- 挑战高薪的程序员建议看的文章
- delegate block 通知
- android UI学习 -- 设置界面的布局(包括style的使用,selector的使用,Checkbox自定义样式,菜单项的样式)
- 微信测试号的URL和Token原理
- 在c的函数中调用oc的方法
- HDFS 解析
- jQuery拖放排序插件DDSort
- Android十大常用技术揭秘-挑战
- 关于spring-security登录后重定向至拦截前访问的url的实现原理
- 【HNOI2011】【BZOJ2336】任务调度
- Java ConcurrentModificationExcepton