为Android Studio中的SettingsActivity定制任务栏
Android Studio为开发者提供了很多内建的Activity, 其中Settings Activity是非常有用且功能强大的一种内建Activity.
Setting Activity其实本质上是从PreferenceActivity中继承过来的。使用Setting Activity后,完全不需要自己控制Preferences的读写,PreferenceActivity会帮我们处理一切。
PreferenceActivity和普通的Activity不同,它不再使用普通的界面布局文件,而是使用选项设置的布局文件。选项设置的布局文件以PreferenceScreen作为根元素,每一个PreferenceScreen对应后台的一个PreferenceFragment。
使用Android Studio添加一个Activity,会默认帮我们生成一个Pref_header.xml文件和若干个Pref*.xml文件。对应到Activity里,需要对应定义几个PreferenceFragment和重写onBuildHeaders方法用于载入定义在Pref_header.xml中的入口布局。
相应的代码片段如下
Pref_header.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="com.example.xpshen.myapplication.SettingsActivity$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/pref_header_general" />
<header
android:fragment="com.example.xpshen.myapplication.SettingsActivity$NotificationPreferenceFragment"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/pref_header_notifications" />
<header
android:fragment="com.example.xpshen.myapplication.SettingsActivity$DataSyncPreferenceFragment"
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/pref_header_data_sync" />
</preference-headers>

Pref_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:capitalize="words"
android:defaultValue="@string/pref_default_display_name"
android:inputType="textCapWords"
android:key="example_text"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_display_name" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_add_friends_to_messages" />
</PreferenceScreen>

SettingActivity.java

public class SettingsActivity extends AppCompatPreferenceActivity {
...
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
...
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("example_text"));
bindPreferenceSummaryToValue(findPreference("example_list"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}

这样一个setting activity就可以工作了。
但是此类activity由于不使用普通的界面布局文件,我们无法在布局文件中添加自定以的控件。
比如我们想要在页面的底部添加一个任务栏,其实是无法简单的通过修改布局文件来增加的。
本文采用的方法是基于下面文章的思路来的。
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0331/1608.html
基本的思路是,在Setting Activity的onCreate方法中,截获之前布局树上的content元素,插入我们自定义的底部任务栏。
代码如下
SettingActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
hookThebottomBar();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
} private void hookThebottomBar(){
ViewGroup content = (ViewGroup) findViewById(android.R.id.content);
LayoutInflater.from(this).inflate(R.layout.com_bottombar, content, true);
}

com_bottombar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" /> </LinearLayout>

这里注意,我们拦截到的content其实是Framelayout,而我们的目标是添加一个底部任务栏,所以需要在上面的com_bottombar.xml设置 android:layout_gravity="bottom",这样这个我们后续添加的帧才不会覆盖之前的内容。
最后的效果图如下

为Android Studio中的SettingsActivity定制任务栏的更多相关文章
- Android Studio中如何使用自定义的framework库
在安卓app开发中,通常不会遇到需要使用自定义framework库的情况,使用的都是标准的内核库.但也有例外,比如针对定制化的ROM,ROM厂商可能在ROM中对安卓源码做过修改,对应用层app暴露出与 ...
- android studio 中移除module和恢复module
一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...
- Android Studio中Button等控件的Text中字符串默认大写的解决方法
初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...
- .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口
对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...
- 如何将Eclipse中的项目迁移到Android Studio 中
如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...
- Android开发的小技巧,在Android Studio中使用Designtime Layout Attributes
在编写xml文件时,为了预览效果,经常会使用默认填上一些内容,比如TextView时,随便写上一个text <TextView ... android:text="Name:" ...
- 在android studio 中使用applicationid的问题
现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...
- Android studio 中的配置编译错误总结
1.编译Andorid 工程的时候,有时候出现gradle 报下面的错误. Error:(1, 0) Cause: com/android/build/gradle/LibraryPlugin : U ...
- Android Studio中清单文件改versionCode和versionName没效果的原因
在Android Studio中,项目的versionCode 和versionName 的控制不是在AndroidManifest.xml清单文件中更改的,而是在项目的build.gradle中更改 ...
随机推荐
- 初涉扫码登录:edusoho实现客户端扫码登录(简版)
一.项目简介及需求 edusoho是一套商业版的在线教育平台,项目本身基于symfony2框架开发,现在有一款自己的APP,要求在不多修改edusoho自身代码的基础上,实现客户端对PC端扫码登录.不 ...
- LeetCode - 307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- redis新手入门,摸不着头脑可以看看<一>
公司在用redis,但我并不会.所以需求就来了.. redis的好处坏处自己百度就好我也不去复制黏贴了. ----------------------------------------------- ...
- centos6下从源码安装setuptools和pip
1. 下载setuptools及pip的源码包 setuptools与pip都是python的模块 setuptools源码包: https://pypi.python.org/pypi/setupt ...
- 用户不在sudoers 文件中。此事将被报告 or (usermod:“sudo”组不存在)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 异常处理汇总-服 务 器 http://www.cnblogs.com/dun ...
- 码农很忙代理IP系统V1.0版本上线
码农很忙代理IP系统V1.0版本上线 经过为期一个月的重写和测试,新版本的码农很忙代理IP系统已于今日正式上线.新版本拥有更精准的匿名类型识别和更高效的验证调度算法. 新版本仍旧采用ASP.NET B ...
- TzObjectInspector 一例
TzObjectInspector Github上的一个开源组件!可以做到类似Delphi IDE属性,事件面板的样式!作者持续更新中... 看起来是这个样子: 这个东西用起来并不像想象的那样可以直接 ...
- Git (gnome-ssh-askpass:3871): Gtk-WARNING **: cannot open display:
在使用Git在客户端使用git push 命令提交文件到github时,出现报错 (gnome-ssh-askpass:): Gtk-WARNING **: cannot open display: ...
- dubbox系列【一】——dubbox简介
1.dubbox是什么? dubbox是当当网开源的开源分布式服务框架,基于阿里巴巴dubbo. 1个框架 + 2个方案:分布式服务框架 + RPC远程调用方案 + SOA服务治理方案. 2.dubb ...
- 剑指offer第八天
32.把数组排成最小的数 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323 ...