标题栏自定义
如果不想用Android默认的标题栏,可以自定义标题栏,这样可以在标题栏上添加一些控件,比如左上角添加返回控件(这个Android默认通过配置也有),添加标题内容,右上角添加一个下拉菜单等。
要想自定义标题栏,需要先在要取消自定义标题栏的activity的风格设置中增加
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
这个需要放到styles.xml文件中,在AndroidManifest.xml中引用
android:theme="@style/AppTheme.NoActionBar"
然后定义自己的标题栏
<android.support.v7.widget.Toolbar
android:id="@+id/department_main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.AppBarOverlay"
app:theme="@style/MyCustomToolBarStyle">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"/>
</android.support.v7.widget.Toolbar>
这个标题栏中有一个文本框。
还需要定义一个下拉菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/department_refresh"
android:title="更新条目"/>
</menu>
通过如下方式在activity中设置自定的标题栏
toolbar=(Toolbar) findViewById(R.id.department_main_toolbar);
setSupportActionBar(toolbar);
titleTxt = (TextView)findViewById(R.id.toolbar_title);
titleTxt.setText("测试");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
还有右上角的下拉菜单
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
getMenuInflater().inflate(R.menu.menu_department_refresh,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.department_refresh:
Toast.makeText(this,"更新界面",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
设置界面
在Android中写一个配置相关的界面,不需要通过layout画界面的方式实现,就可以实现单选,多选,edit,checkbox等任何设置。
用到的布局activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent" android:layout_height="match_parent">
</FrameLayout>
首先,在activity中启动fragment
public class PreferencesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
// 显示设置界面
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, new PreferencesFragment()).commit();
}
}
在xml中定义需要使用的各项配置,preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="user_setting"
android:summary="系统设置"
android:title="系统设置">
<ListPreference
android:title="AAA"
android:entryValues='@array/result_value'
android:entries='@array/result_value'
android:key='result_value'/>
</PreferenceCategory>
<PreferenceCategory
android:key="result_setting"
android:summary="用户设置"
android:title="用户设置">
<ListPreference
android:key="lower_limit"
android:summary="CCC"
android:title="CCC"
android:entries="@array/lower_limit"
android:entryValues="@array/lower_limit"/>
</PreferenceCategory>
<PreferenceCategory
android:key="query_setting"
android:summary="多选配置项"
android:singleLineTitle="true"
android:title="多选配置项">
<MultiSelectListPreference
android:key="query_condition"
android:summary="多选配置项"
android:title="多选配置项"
android:entries="@array/condition"
android:entryValues="@array/condition_index"/>
</PreferenceCategory>
</PreferenceScreen>
然后在fragment中读取用xml文件定义的各项配置
public class PreferencesFragment extends PreferenceFragment {
private SharedPreferences share;
SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Bundle bundle = new Bundle();
if(key.equals("result_value"))
findPreference(key).setSummary(prefs.getString(key,"default"));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 添加要显示的配置文件
addPreferencesFromResource(R.xml.preferences);
// 设置配置文件的名称
getPreferenceManager().setSharedPreferencesName("my_setting");
//读取配置文件,并写到界面上
share = getActivity().getSharedPreferences("my_setting", MODE_PRIVATE);
findPreference("result_value").setSummary(share.getString("result_value","default"));
//设置默认勾选值
ListPreference listPreference1 = (ListPreference)findPreference("result_value");
listPreference1.setValue(share.getString("result_value","default"));
share.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(listener);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(listener);
}
}
随时写入和读出的配置
比如在登陆时把当前用户写入到配置文件中,在转到个人信息页面时再把当前用户读出来。
写入配置
PropertiesUtil.setProperties("current_user","admin");
读出配置
String name = PropertiesUtil.getProperties("current_user");
PropertiesUtil的实现:
public class PropertiesUtil {
private static Properties properties;
static {
properties = new Properties();
try {
//方法一:通过activity中的context攻取setting.properties的FileInputStream
// 注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
//InputStream is = BaseApplication.getIns().getAssets().open("appConfig");
//方法二:通过class获取setting.properties的FileInputStream
//这种方法无效,可能是路径问题,大家有思路可以留言
InputStream is = PropertiesUtil.class.getResourceAsStream("/assets/appConfig.properties");
properties.load(is);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getProperties (String key){
return properties.getProperty(key);
}
public static void setProperties(String key,String value){
properties.setProperty(key,value);
}
}
只有单个fragment的activity基类
activity_fragment.xml布局实现
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent" android:layout_height="match_parent">
</FrameLayout>
定义一个抽象基类
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
protected int getLayoutResId() {
return R.layout.activity_fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
在activity中,使用方式如下:
public class MyActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new MyFragment();
}
}
最近还有很多的开发技术,限于篇幅,留作下次。还有一个树型控件的Android需求以及调用c++代码的需求,到时候一并整理。