The final result is as follows:
Specifically, the checkbox custom selection and selection style are implemented. The menu item sets different backgrounds based on different locations. First, the overall layout file code is:
<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 = "enable notification"/> <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 = "enable ringtone"/> <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 = "enable vibration"/> <checkbox style = "@ style/mycheckbox" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>
Note:
Style = "@ style/mysettingtext"
Style is used here. XML file to control the style. in actual development of Android, the attributes of some components are often repeated. If each control needs to input these attributes separately, it will be inefficient. you can add duplicate code to style. in XML, set it to a style. When using these attributes, You can reference this style. style = "@ style/mysettingtext" indicates the attribute of text. The Code is as follows:
<style name="MySettingText"> <item name="android:layout_margin">10dp</item> <item name="android:layout_weight">6</item></style>
Only two attributes are used here. you can modify it as needed during development. in the future, you do not need to add these attributes to the text of each setting item. Just reference this style. selector is used for checkbox style customization.
Style = "@ style/mycheckbox"
Code corresponding to this sentence:
<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>
Note this sentence:
<Item name = "Android: button"> @ drawable/check </item>
The selector custom style is used here.
Create a check (custom) XML file under Res/drawable. The Code is as follows:
<?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>
Where
@ Drawable/on indicates the checkbox image when selected, and @ drawable/off indicates the opposite.
We also noticed that the top of the first item in the menu is the rounded corner, the middle item is the square with the four corners, and the last item is the lower corner. This is achieved by setting different background images, which are not detailed.
Reprint please indicate the source http://blog.csdn.net/shimiso
Technical Exchange Group: 173711587