2012-08-03 3 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> 

코드는 이미지 뷰의 컨텐츠

@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

을 변경하려면 무엇을 그릴 수 고해상도/아래에있는 모든 이미지를 넣고 R.drawable를 통해 액세스하는 경우. ? 이 접근 방식은 항상 저에게 효과적이었습니다. URI 사용에 대해 설득력있는 이유가 없다면이 접근법을 시도해 볼 가치가 있습니다. URI를 사용해야하는 경우에도이 방법을 테스트하고 작동 시키도록 제안합니다. 그럼 적어도 당신은 일반적으로 당신의 구현이 아니라 '경로'의 가치에 문제가 있다는 것을 알게 될 것입니다.

+0

이미지를 카메라 또는 sdcard에서 가져올 수 있으며 사용자가 원하는 언제든지 변경할 수 있으므로 드로어 솔루션을 사용할 수 없습니다. 이 코드는 기본 설정을 수정하고, 사용자 정의 레이아웃의 일부 텍스트를 변경하려고 시도했으며 setText ("newText")를 호출 한 후; 디버그하는 동안 TextView에 새 텍스트가 있지만 일단 완료되면 원래 텍스트로 돌아갑니다. – Giuseppe

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입니다 :

<PreferenceCategory 
    android:key="@string/pref_category_myNote_key" 
    android:title="@string/pref_category_myNote" > 
    <com.flounder.xeniaNote.view.ImageViewPreference 
     android:key="@string/pref_image_key" 
     android:title="@string/pref_image" 
     android:widgetLayout="@layout/pref_account_image" /> 
</PreferenceCategory> 

그리고 mImagePref.setImage으로 전화하여 ImageView 콜백 메소드 onPreferenceClick을 변경하십시오.

도움이 되었기를 바랍니다. 행운을 빕니다.

+0

findViewById()는 여전히 onBindView()에서 작동하지 않는다는 것을 알게되었습니다. 나는 대신'onCreateView()'에서 그것을했는데, 그것은 효과가 있었다. – ryan

+0

@ryan 이상합니다. 나는이 문제가 없으며 설명서에 '레이아웃에서 사용자 정의 뷰에 대한 참조를 가져 와서 속성을 설정하는 좋은 장소입니다.'라고 말합니다. http://developer.android.com/reference/android/preference/Preference.html#onBindView%28android.view.View%29 – Sufian