Android 偏好设置如何进行
在我的活动之一的 onCreate 中,我使用以下方法来获取列表视图。所应用的 xml 布局来自 xml 文件“country_row”。现在我想使用共享首选项来更改列表视图的一些布局,例如应保留的字体颜色、背景颜色。据我所知,我可以使用共享首选项来实现这一点。但假设我知道在 xml 文件中定义它的方法,在本节中,我如何应用一些更改,例如使用相同的“country_row”xml 文件应用不同的字体颜色或背景颜色。或者我必须完全定义新的 xml 文件吗?我是怎么迷茫的。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before
if(sort == 1)
sortOrder = "year";//sort on year
else if (sort == 2)
sortOrder = "country";//sort on country
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row, c, from, to);
setListAdapter(sca);
}
In one of my activity's onCreate I am using the following to get the list view. The xml layout being applied is from xml file "country_row". Now I want to use shared preferences to change some of the layout of my listview for eg font color, background color which should be preserved. As I know I can achieve this using the shared preferences. But assuming that I know the way to define it in xml file, in this section how can I apply some of the changes say different font color or background color using the same "country_row" xml file. Or do I have to completely define new xml file? What is the way I am confused.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before
if(sort == 1)
sortOrder = "year";//sort on year
else if (sort == 2)
sortOrder = "country";//sort on country
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row, c, from, to);
setListAdapter(sca);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将是快速而肮脏的:
干净的解决方案是创建您自己的适配器并在那里进行。
This would be quick and dirty:
The clean solution is to create your own adapter and do it there.