2012-08-03 100 views
5

我有一個圖像和兩個文本的自定義首選項,我需要以編程方式更改圖像。如何管理自定義首選項佈局

我能夠得到自定義首選項視圖,並使用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); 

回答

0

如果你把所有的圖像下RES /繪製,然後通過R.drawable訪問它們。 ?這種方法一直爲我工作。如果你沒有使用URI的令人信服的理由,這種方法可能值得嘗試。即使你必須使用URI,我也會建議測試這種方法並使其工作。那麼至少你會知道「路徑」的價值有問題,而不是你的實現。

+0

我無法使用drawable解決方案,因爲圖像將從相機或SD卡中選取,並且可以隨時更改用戶所需的時間。代碼解決了首選項問題,我也嘗試在自定義佈局中更改一些文本,並在調用setText(「newText」);在調試過程中,TextView會有新的文本,但一旦完成,它就會以原始形式返回。 – Giuseppe 2012-08-03 09:03:27

6

我試圖getView常規,但它不爲我工作,我終於得到它通過使用自定義Preference

public class ImageViewPreference extends Preference { 
    private ImageView mImageView; 
    private Bitmap mPhoto; 

    public ImageViewPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.setWidgetLayoutResource(R.layout.pref_account_image); 
     if (mPhoto == null) { 
      mPhoto = BitmapFactory.decodeResource(
        getContext().getResources(), R.drawable.icon); 
     } 
    } 

    @Override 
    protected void onBindView(View view) { 
     super.onBindView(view); 
     mImage = (ImageView) view.findViewById(R.id.pref_imageView); 
     mImage.setImageBitmap(mPhoto); 
    } 

    public void setImage(Intent imageData) { 
     mPhoto = data.getParcelableExtra("data"); 
    } 
} 

這裏是Preference.xml:

而且致電mImagePref.setImage在回撥方式onPreferenceClick中更改您的ImageView

我希望它有幫助!祝你好運。

+0

我發現'findViewById()'仍然不能在'onBindView()'中工作。我在'onCreateView()'中做了它,而不是工作。 – ryan 2013-09-20 01:57:15

+0

@ryan這很奇怪。我沒有這個問題,文檔說'這是一個很好的地方來獲取對佈局和自定義屬性的自定義視圖的引用。 http://developer.android.com/reference/android/preference/Preference.html#onBindView%28android.view.View%29 – Sufian 2014-08-25 09:24:19