How to manage custom preference layout

前端 未结 2 1272
南笙
南笙 2021-01-03 01:50

I have a custom preference with an image and two text and I need to change the image programmatically.

I am able to get the custom preference view and to find the Im

相关标签:
2条回答
  • 2021-01-03 01:59

    I tried getView routine but it doesn't work for me, finally I got it through with a custom 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");
        }
    }
    

    And here is the 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>
    

    And call mImagePref.setImage to change your ImageView in the callback method onPreferenceClick.

    I wish it helps! Good luck.

    0 讨论(0)
  • 2021-01-03 02:02

    What if you put all images under res/drawable and then access them through R.drawable. ? This approach has always worked for me. If you don't have compelling reasons for using URI this approach might be worth trying. Even if you have to use URI I would suggest to test this approach and get it working. Then at least you would know that there is something wrong with the value of the 'path' and not your implementation in general.

    0 讨论(0)
提交回复
热议问题