我有一個圖像和兩個文本的自定義首選項,我需要以編程方式更改圖像。如何管理自定義首選項佈局
我能夠得到自定義首選項視圖,並使用findViewById在佈局中查找ImageView,但如果我嘗試更改圖像,則不起作用。
我錯了什麼嗎?
偏好佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageforfacebook"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dip"
android:src="@drawable/icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fadingEdge="horizontal"
android:singleLine="true"
android:text="titolo"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="18sp" />
<TextView
android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/title"
android:layout_below="@android:id/title"
android:maxLines="2"
android:text="descrizione"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
密碼,才能更改ImageView的內容
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
View v = (View) prefs_facebookimage.getView(null, null);
ImageView img = (ImageView) v.findViewById(R.id.imageforfacebook);
Uri uri = Uri.parse(path);
img.setImageURI(uri);
我無法使用drawable解決方案,因爲圖像將從相機或SD卡中選取,並且可以隨時更改用戶所需的時間。代碼解決了首選項問題,我也嘗試在自定義佈局中更改一些文本,並在調用setText(「newText」);在調試過程中,TextView會有新的文本,但一旦完成,它就會以原始形式返回。 – Giuseppe 2012-08-03 09:03:27