我已經將共享首選項的所有內容都放在適當位置,並且在我的一個活動中,我也能夠在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。那麼我怎樣才能使用這個佈局的偏好值。我從複選框獲得的布爾值是正確的,因爲我也打印出來看它。但是當我嘗試像上面這樣做時,它不起作用,程序崩潰,說空指針異常。
我在這裏卡住的東西是....我得到正確的偏好值,但不知道如何使用它或將其應用到我現有的佈局...或者我需要做出不同的佈局。 ..我不確定。
我試過但不工作 – SASM
它有什麼問題?要存儲顏色,最好使用int變量。 – Huang
我從共享偏好中獲得的值也持續存在,當我上次看到複選框時,它就像我離開(開/關)一樣。我也可以通過getBoolean或getInt獲取值。但是我在上面給出的country_row xml佈局在我當前正在應用的問題中。我也知道如何獲得findViewById(R.id.layoutID)。問題是如何改變字體顏色或bg顏色與我得到的偏好值,以便country_row xml將顯示此更改的佈局。 – SASM