android4.0 Settings 界面修改
- 格式:pdf
- 大小:245.59 KB
- 文档页数:1
android4.0 Settings 界面修改项目需要修改4.0的settings,先写点界面修改部分的实现吧n n n n nn n n n 一、上面的分页tab n android3.0以后就加入了ActionBar,上面的那条是一个ActionBar,不熟悉的可以先去看看ActionBar的介绍n 在Settings.java的onCreate()方法中添加,代码如下: n n [java]n ActionBar ab = getActionBar(); n n n n n ab.setNavigationMode (ActionBar.NAVIGATION_MODE_TABS); n n n n n ab.setDisplayOptions(3); n n n n n Tab tab1 = ab.newTab(); n n n n ntab1.setTabListener(new MyTabListener()); n n n n n tab1.setText(R.string.settings_tab_individuation); n n ActionBar ab = getActionBar(); n n n n ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); n n n n ab.setDisplayOptions(3); n n n n Tab tab1 = ab.newTab(); n n n n tab1.setTabListener(new MyTabListener()); n n n n tab1.setText(R.string.settings_tab_individuation); 二、左侧Header背景及list item的背景更换n 1、list item的背景在Settings.java的内部类HeaderAdapter的getView()方法中进行设定,准备好自己的selector,参考代码如下: n n n n n [java]n if (holder.title.getText().equals(getContext().getResources().getString(R.string.header_category_wireless_networks))) { n n n n n n n n n view.setBackgroundResource(R.drawable.selector_list_item_top); n n n n n n n } else { n n n n n n n n n view.setBackgroundResource(R.drawable.selector_list_item_mid); n n n n n n n } n n n n n n n nn n n n n n n return view; n n if (holder.title.getText ().equals(getContext().getResources().getString(R.string.header_category_wireless_networks))) { n n n n n nview.setBackgroundResource(R.drawable.selector_list_item_top); n n n n n n } else { n n n n n n view.setBackgroundResource(R.drawable.selector_list_item_mid); n n n n n n } n n n n n nn n n n n n n return view;2、背景的更换(即上图中左侧list下面的红色背景) n 通过查看PreferenceActivity的源码,再查看对应的布局文件,找到list后面的layout的对应的id,在Settings.java的onCreate()方法中进行背景的更换就可以了 n 参考代码如下: n n [java]n View v1 = findViewById(com.android.internal.R.id.prefs); n n n n nv1.setBackgroundColor(Color.RED); n n View v1 = findViewById(com.android.internal.R.id.prefs); n n n n v1.setBackgroundColor (Color.RED);3、左侧蓝色背景的更换n 方法和左侧的背景更换一样,查看源码,找到对应的布局id,在Settings.java的onCreate()方法中进行背景的更换就可以了,参考代码如下: n n [java] View v = findViewById(com.android.internal.R.id.prefs_frame); n n n n nv.setBackgroundColor(Color.BLUE); n n View v = findViewById(com.android.internal.R.id.prefs_frame); n n n n v.setBackgroundColor (Color.BLUE);4、添加一点,右则的settings item的背景更换,右侧的item就是PreferenceScreen里面对应的Preference,看代码: n n [html]n <CheckBoxPreference n n n n n n n android:defaultValue="true" n n n n n n n android:key="set_01" n n n n n n nandroid:layout="@layout/pref_list_item_top" n n n n n n n android:summaryOff="当前状态:关闭" n n n n n n nandroid:summaryOn="当前状态:开启" n n n n n n n android:title="提示-01" n n n n n n nandroid:widgetLayout="@layout/pref_widget_checkbox" /> n nn n n n n <Preference n n n n n n n android:key="set_02" n n n n n n n android:layout="@layout/pref_list_item_mid" n n n n n n n android:summary="点击修改" n n n n n n n android:title="设置-02" n n n n n n n android:widgetLayout="@layout/pref_widget_more" /> n n <CheckBoxPreference n n n n n n android:defaultValue="true" n n n n n n android:key="set_01" n n n n n n android:layout="@layout/pref_list_item_top" n n n n n n android:summaryOff="当前状态:关闭" n n n n n n android:summaryOn="当前状态:开启" n n n n n n android:title="提示-01" n n n n n nandroid:widgetLayout="@layout/pref_widget_checkbox" /> n n n n n <Preference n n n n n n android:key="set_02" n n n n n n android:layout="@layout/pref_list_item_mid" n n n n n n android:summary="点击修改" n n n n n n android:title="设置-02" n n n n n n android:widgetLayout="@layout/pref_widget_more" /> preference是可以指定layout和widgetLayout的,注意layout代码中控件的id,一定不可以错,要用系统的,layout代码如下: n n n n n [html]n <?xml version="1.0" encoding="UTF-8"?> n <LinearLayoutxmlns:android="/apk/res/android" n n n android:layout_width="fill_parent" n n nandroid:layout_height="wrap_content" n n n android:background="@drawable/selector_list_item_bot" n n nandroid:gravity="center_vertical" n n n android:minHeight="?android:listPreferredItemHeight" n n n android:paddingRight="? android:scrollbarSize" > n nn n n <RelativeLayout n n n n n android:layout_width="wrap_content" n n n n nandroid:layout_height="wrap_content" n n n n n android:layout_marginBottom="6.0dip" n n n n nandroid:layout_marginLeft="15.0dip" n n n n n android:layout_marginRight="6.0dip" n n n n n android:layout_marginTop="6.0dip" n n n n n android:layout_weight="1.0" > n nn n n n n <TextView n n n n n n n android:id="@android:id/title" n n n n n n n android:layout_width="wrap_content" n n n n n n n android:layout_height="wrap_content" n n n n n n n android:ellipsize="marquee" n n n n n n n android:fadingEdge="horizontal" n n n n n n n android:singleLine="true" n n n n n n nandroid:textColor="@drawable/selector_text_title" n n n n n n n android:textSize="18.0sp" /> n nn n n n n <TextView n n n n n n n android:id="@android:id/summary" n n n n n n n android:layout_width="wrap_content" n n n n n n nandroid:layout_height="wrap_content" n n n n n n n android:layout_alignLeft="@android:id/title" n n n n n n nandroid:layout_below="@android:id/title" n n n n n n n android:maxLines="2" n n n n n n nandroid:textColor="@drawable/selector_text_detail" n n n n n n n android:textSize="14.0sp" /> n n n </RelativeLayout> n nn n n <LinearLayout n n n n n android:id="@android:id/widget_frame" n n n n n android:layout_width="wrap_content" n n n n nandroid:layout_height="fill_parent" n n n n n android:gravity="center_vertical" n n n n n android:orientation="vertical" /> n nn </LinearLayout> n n <?xml version="1.0" encoding="UTF-8"?> <LinearLayoutxmlns:android="/apk/res/android" n n android:layout_width="fill_parent" n nandroid:layout_height="wrap_content" n n android:background="@drawable/selector_list_item_bot" n nandroid:gravity="center_vertical" n n android:minHeight="?android:listPreferredItemHeight" n n android:paddingRight="?android:scrollbarSize" > n n n <RelativeLayout n n n n android:layout_width="wrap_content" n n n nandroid:layout_height="wrap_content" n n n n android:layout_marginBottom="6.0dip" n n n n android:layout_marginLeft="15.0dip" n n n n android:layout_marginRight="6.0dip" n n n n android:layout_marginTop="6.0dip" n n n n android:layout_weight="1.0" > n n n n n <TextView n n n n n n android:id="@android:id/title" n n n n n n android:layout_width="wrap_content" n n n n n nandroid:layout_height="wrap_content" n n n n n n android:ellipsize="marquee" n n n n n n android:fadingEdge="horizontal" n n n n n n android:singleLine="true" n n n n n n android:textColor="@drawable/selector_text_title" n n n n n nandroid:textSize="18.0sp" /> n n n n n <TextView n n n n n n android:id="@android:id/summary" n n n n n nandroid:layout_width="wrap_content" n n n n n n android:layout_height="wrap_content" n n n n n nandroid:layout_alignLeft="@android:id/title" n n n n n n android:layout_below="@android:id/title" n n n n n nandroid:maxLines="2" n n n n n n android:textColor="@drawable/selector_text_detail" n n n n n n android:textSize="14.0sp" /> n n </RelativeLayout> n n n <LinearLayout n n n n android:id="@android:id/widget_frame" n n n n android:layout_width="wrap_content" n n n n android:layout_height="fill_parent" n n n n android:gravity="center_vertical" n n n n android:orientation="vertical" /> n </LinearLayout>widgetlayout的代码如下: n n [html]n <?xml version="1.0" encoding="UTF-8"?> n <CheckBoxxmlns:android="/apk/res/android" n n n android:id="@android:id/checkbox" n n nandroid:layout_width="wrap_content" n n n android:layout_height="wrap_content" n n n android:layout_gravity="center_vertical" n n n android:layout_marginRight="4.0dip" n n n android:button="@drawable/selector_checkbox" n n n android:clickable="false" n n n android:focusable="false" /> n n <?xml version="1.0" encoding="UTF-8"?> <CheckBoxxmlns:android="/apk/res/android" n n android:id="@android:id/checkbox" n nandroid:layout_width="wrap_content" n n android:layout_height="wrap_content" n n android:layout_gravity="center_vertical" n n android:layout_marginRight="4.0dip" n n android:button="@drawable/selector_checkbox" n n android:clickable="false" n nandroid:focusable="false" /> 上面这个是CheckBox的widgetlayout n 如果是普通的preference可以没有,也可以放一个imageView什么的(例如一个向右的箭头)起到指示作用。