2011-10-01 52 views
1

我已經將共享首選項的所有內容都放在適當位置,並且在我的一個活動中,我也能夠在logcat中檢索像這樣的共享首選項值。 String i = prefs.getString(「bgColor」,「#f2345」); System.out.println(i);如何將共享首選項值設置爲佈局

但在這個活動我使用這樣 SimpleCursorAdapter SCA =新SimpleCursorAdapter的佈局(在此,R.layout.country_row, c,由,到);
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。那麼我怎樣才能使用這個佈局的偏好值。我從複選框獲得的布爾值是正確的,因爲我也打印出來看它。但是當我嘗試像上面這樣做時,它不起作用,程序崩潰,說空指針異常。

我在這裏卡住的東西是....我得到正確的偏好值,但不知道如何使用它或將其應用到我現有的佈局...或者我需要做出不同的佈局。 ..我不確定。

回答

1

我想你想要做的是在代碼中動態地改變一些佈局參數。 通常,您可以通過代碼或.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文檔以查找信息。

+0

我試過但不工作 – SASM

+0

它有什麼問題?要存儲顏色,最好使用int變量。 – Huang

+0

我從共享偏好中獲得的值也持續存在,當我上次看到複選框時,它就像我離開(開/關)一樣。我也可以通過getBoolean或getInt獲取值。但是我在上面給出的country_row xml佈局在我當前正在應用的問題中。我也知道如何獲得findViewById(R.id.layoutID)。問題是如何改變字體顏色或bg顏色與我得到的偏好值,以便country_row xml將顯示此更改的佈局。 – SASM

0

像黃回答, 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(); 
     } 

希望它能適合你。如果沒有,然後告訴我。

0

其實我怎麼設法改變視圖的佈局就是這樣做的。 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;  
    } 
}