Giu*_*ppe 5 android android-layout
我有一个图像和两个文本的自定义首选项,我需要以编程方式更改图像.
我能够获取自定义首选项视图并使用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>
Run Code Online (Sandbox Code Playgroud)
代码改变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);
Run Code Online (Sandbox Code Playgroud)
小智 7
我尝试了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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
并致电mImagePref.setImage
更改您ImageView
的回调方法onPreferenceClick
.
我希望它有所帮助!祝好运.
小智 0
如果将所有图像放在 res/drawable 下,然后通过 R.drawable 访问它们会怎么样??这种方法一直对我有用。如果您没有令人信服的理由使用 URI,那么这种方法可能值得尝试。即使您必须使用 URI,我也建议您测试此方法并使其正常工作。那么至少您会知道“路径”的值有问题,而不是您的总体实现有问题。
归档时间: |
|
查看次数: |
7359 次 |
最近记录: |