我有一个图像和两个文本的自定义首选项,我需要以编程方式更改图像。如何管理自定义首选项布局
我能够得到自定义首选项视图,并使用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