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