如何将共享首选项值设置为布局

发布于 2024-12-07 09:25:55 字数 1763 浏览 4 评论 0原文

我已经将有关共享首选项的所有内容都放在适当的位置,并且在我的一项活动中,我还可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-12-14 09:25:55

我认为你想要做的是在代码中动态更改一些布局参数。
通常,您可以通过代码或 .xml 文件为视图(Layout、TextView、EditText 等)设置属性。
以布局为例。
首先向布局添加一个 id 属性。

<LinearLayoout android:id="@+id/layoutID" .../>

那么

LinearLayout layout=(LinearLayout)findViewById(R.id.layoutID) //get the layout object.
layout.setBackgroundColor (color from your preferences);

上面就是基本思想了。请阅读 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.

<LinearLayoout android:id="@+id/layoutID" .../>

Then

LinearLayout layout=(LinearLayout)findViewById(R.id.layoutID) //get the layout object.
layout.setBackgroundColor (color from your preferences);

Above is the basic idea. Please read SDK document to find mor info.

等待我真够勒 2024-12-14 09:25:55

就像黄老师的回答一样,
1) 您应该为必须更改其属性的布局或视图创建引用。
2)之后将首选项的值放入相应的变量中。
3) 将值设置为该布局或视图以获得效果。

它一定会起作用的。有关更多参考信息,请参阅下面的示例,了解在其中选择复选框时我会执行一些操作(例如播放音乐)。

myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
    lessResultSound = myPrefs.getBoolean("LessResultIsOn", false);

    System.out.println("============================= The FullResultSound in Result Page is: "+fullResultSound);
    System.out.println("============================= The LessResultSound in Result Page is: "+lessResultSound);


if(fullResultSound)
        {
            playSound(soundFileForFullResult);
        }
        else
        {
            playSound();
        }

希望它对你有用。如果没有那就告诉我。

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.

myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
    lessResultSound = myPrefs.getBoolean("LessResultIsOn", false);

    System.out.println("============================= The FullResultSound in Result Page is: "+fullResultSound);
    System.out.println("============================= The LessResultSound in Result Page is: "+lessResultSound);


if(fullResultSound)
        {
            playSound(soundFileForFullResult);
        }
        else
        {
            playSound();
        }

Hope it will works for you. If not then tell me.

温柔少女心 2024-12-14 09:25:55

实际上我是如何改变视图布局的。
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 MySimpleCursorAdapter(this, R.layout.country_row,
            c, from, to);  
    setListAdapter(sca);

class MySimpleCursorAdapter  extends SimpleCursorAdapter{

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        // TODO Auto-generated constructor stub
    }


    @Override   // Called when updating the ListView
    public View getView(int position, View convertView, ViewGroup parent) {
        /* Reuse super handling ==> A TextView from R.layout.list_item */
        View v =  super.getView(position,convertView,parent); 

        TextView tYear = (TextView) v.findViewById(R.id.year);
        TextView tCountry = (TextView) v.findViewById(R.id.country);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        boolean font_size = prefs.getBoolean("fontSize", false);
        boolean italic_font = prefs.getBoolean("fontItalic", false);


        String listpref = prefs.getString("bgColor", "#ffffff80");
        //System.out.println(listpref);
        tYear.setBackgroundColor(Color.parseColor(listpref));
        tCountry.setBackgroundColor(Color.parseColor(listpref));


        if (font_size){
            tYear.setTextSize(25);
            tCountry.setTextSize(25);
        }


        if (italic_font){
            tYear.setTypeface(null, Typeface.ITALIC);
            tCountry.setTypeface(null, Typeface.ITALIC);
        }

        //tv.setBackgroundColor(col);

        return v;       
    }
}

Actually how I managed to change the layout of the view was by doing this.
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 MySimpleCursorAdapter(this, R.layout.country_row,
            c, from, to);  
    setListAdapter(sca);

class MySimpleCursorAdapter  extends SimpleCursorAdapter{

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        // TODO Auto-generated constructor stub
    }


    @Override   // Called when updating the ListView
    public View getView(int position, View convertView, ViewGroup parent) {
        /* Reuse super handling ==> A TextView from R.layout.list_item */
        View v =  super.getView(position,convertView,parent); 

        TextView tYear = (TextView) v.findViewById(R.id.year);
        TextView tCountry = (TextView) v.findViewById(R.id.country);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        boolean font_size = prefs.getBoolean("fontSize", false);
        boolean italic_font = prefs.getBoolean("fontItalic", false);


        String listpref = prefs.getString("bgColor", "#ffffff80");
        //System.out.println(listpref);
        tYear.setBackgroundColor(Color.parseColor(listpref));
        tCountry.setBackgroundColor(Color.parseColor(listpref));


        if (font_size){
            tYear.setTextSize(25);
            tCountry.setTextSize(25);
        }


        if (italic_font){
            tYear.setTypeface(null, Typeface.ITALIC);
            tCountry.setTypeface(null, Typeface.ITALIC);
        }

        //tv.setBackgroundColor(col);

        return v;       
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文