如何将共享首选项值设置为布局
我已经将有关共享首选项的所有内容都放在适当的位置,并且在我的一项活动中,我还可以在 logcat 中检索共享首选项值,如下所示。
String i = prefs.getString("bgColor", "#f2345");
System.out.println(i);
但在本次活动中,我使用这样的布局
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row,c, from, to);
setListAdapter(sca);
,其中“country_row”是我的 xml 布局文件,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff000099"
android:background="#ffffff80"
android:padding="10dp"
android:textSize="16sp"
android:text="1964"/>
<TextView android:id="@+id/country"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff80"
android:background="#ff000099"
android:padding="10dp"
android:textSize="16sp"
android:text="Sweden"/>
现在使用我已经从首选项中获得的值,我想在此处进行更改,例如显示的背景颜色或字体大小。我现在想要的只是将这些共享首选项值暗示到我的布局 xml 文件中。我怎样才能做到这一点,我做不到?
实际上我可以从共享首选项中获取值,如下所示
boolean i = prefs.getBoolean("fontBold", false);
System.out.println(i);
if (i){
TextView tv = (TextView)findViewById(R.id.year);
tv.setTypeface(null, Typeface.BOLD);//null pointer
}
在光标适配器中我已经应用了布局country_row.xml。那么我如何使用这个布局的首选项值。我从复选框获得的布尔值是正确的,因为我也打印出来查看它。但是,当我尝试执行上面的操作时,它不起作用,并且程序崩溃并显示空指针异常。
我被困在这里的是......我得到了正确的首选项值,但不知道如何使用它或将其应用到我现有的布局......或者我需要制作不同的布局......我是不确定。
I have already put everything about shared preferences in place and in one of my activity I am also able to retrieve the shared preferences values like this in logcat.
String i = prefs.getString("bgColor", "#f2345");
System.out.println(i);
But in this activity I am using a layout like this
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row,c, from, to);
setListAdapter(sca);
where "country_row" is my xml layout file which is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff000099"
android:background="#ffffff80"
android:padding="10dp"
android:textSize="16sp"
android:text="1964"/>
<TextView android:id="@+id/country"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff80"
android:background="#ff000099"
android:padding="10dp"
android:textSize="16sp"
android:text="Sweden"/>
Now using the values that I am already getting from the preferences, I want to change here for example background color or font size being displayed. All I want now is just to imply these shared prefernces values to my layout xml file. How can I do that, I am not being able to do that?
Actually I can get the values from shared preferences like this
boolean i = prefs.getBoolean("fontBold", false);
System.out.println(i);
if (i){
TextView tv = (TextView)findViewById(R.id.year);
tv.setTypeface(null, Typeface.BOLD);//null pointer
}
In the cursor adapter I am already applying a layout country_row.xml. So how can I use the preferences value I am getting to this layout. The boolean value that I am getting from checkbox is correct as I have also printed out to see it. But when I try to do like above it doesn't work and the program crashes saying the null pointer exception.
The thing I am stuck here is....I am getting the correct preferences value but don't know how to use it or apply it to my existing layout...or do i need to make different layout...i am not sure about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你想要做的是在代码中动态更改一些布局参数。
通常,您可以通过代码或 .xml 文件为视图(Layout、TextView、EditText 等)设置属性。
以布局为例。
首先向布局添加一个 id 属性。
那么
上面就是基本思想了。请阅读 SDK 文档以查找更多信息。
I think what you want to do is to change some layout parameters dynamically in codes.
Generally, you can set attributes to a view(Layout, TextView,EditText,etc.) either by code or by .xml file.
Take a layout as example.
First add an id attribute to the layout.
Then
Above is the basic idea. Please read SDK document to find mor info.
就像黄老师的回答一样,
1) 您应该为必须更改其属性的布局或视图创建引用。
2)之后将首选项的值放入相应的变量中。
3) 将值设置为该布局或视图以获得效果。
它一定会起作用的。有关更多参考信息,请参阅下面的示例,了解在其中选择复选框时我会执行一些操作(例如播放音乐)。
希望它对你有用。如果没有那就告诉我。
As like Huang answer,
1) You should have to create the reference for the layout or view to whom you have to change the properties.
2) After that get the preference's value in to respective variable.
3) Set the value to that layout or view to get effect.
It will surly going to work. For more reference see the below Example for the in which selection of checkbox i do some action like to play Music.
Hope it will works for you. If not then tell me.
实际上我是如何改变视图布局的。
ContentResolver contentResolver = getContentResolver();
Actually how I managed to change the layout of the view was by doing this.
ContentResolver contentResolver = getContentResolver();