8

画像と2つのテキストを使用したカスタム設定があり、プログラムで画像を変更する必要があります。

カスタム設定ビューを取得し、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);
4

2 に答える 2

12

ルーチンを試し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あなたを変更するために呼び出します。ImageViewonPreferenceClick

役に立てば幸いです!幸運を。

于 2012-12-27T12:47:52.627 に答える
0

すべての画像を res/drawable の下に置き、R.drawable を介してそれらにアクセスするとどうなるでしょうか。? このアプローチは常に私にとってうまくいきました。URI を使用する説得力のある理由がない場合は、このアプローチを試してみる価値があります。URI を使用する必要がある場合でも、このアプローチをテストして機能させることをお勧めします。そうすれば、少なくとも「パス」の値に問題があり、一般的な実装ではないことがわかります。

于 2012-08-03T07:53:26.180 に答える