如何管理自定义preference布局 [英] How to manage custom preference layout

查看:98
本文介绍了如何管理自定义preference布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义preference用图像和两个文本,我需要以编程方式改变形象。

我能够获得自定义preference观,并找到使用findViewById ImageView的布局,但如果我尝试改变形象这不工作。

难道我说错了什么吗?

preFERENCE布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>


    < ImageView的
        机器人:ID =@ + ID / imageforfacebook
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:填充=5dip
        机器人:SRC =@可绘制/图标/>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>

        <的TextView
            机器人:ID =@ +安卓ID /标题
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:fadingEdge =横向
            机器人:单线=真
            机器人:文本=TITOLO
            机器人:textAppearance =机器人:ATTR / textAppearanceSmall
            机器人:TEXTSIZE =18sp/>

        <的TextView
            机器人:ID =@ +安卓ID /摘要
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignLeft =@机器人:ID /标题
            机器人:layout_below =@机器人:ID /标题
            机器人:MAXLINES =2
            机器人:文本=descrizione
            机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?
    < / LinearLayout中>

< / LinearLayout中>
 

code要更改ImageView的内容

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){

视图V =(查看)prefs_facebookimage.getView(NULL,NULL);
ImageView的IMG =(ImageView的)v.findViewById(R.id.imageforfacebook);
开放的我们的uri = Uri.parse(路径);
img.setImageURI(URI);
 

解决方案

我试过 getView 常规,但它不为我工作,我终于通过了把它自 preference

 公共类的ImageView preference扩展preference {
    私人ImageView的mImageView;
    私人位图mPhoto;

    公共ImageView的preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        this.setWidgetLayoutResource(R.layout pref_account_image);
        如果(mPhoto == NULL){
            mPhoto = BitmapFactory.de codeResource(
                    的getContext()getResources(),R.drawable.icon)。
        }
    }

    @覆盖
    保护无效onBindView(查看视图){
        super.onBindView(视图);
        mImage =(ImageView的)view.findViewById(R.id. pref_imageView);
        mImage.setImageBitmap(mPhoto);
    }

    公共无效setImage(意向为imageData){
        mPhoto = data.getParcelableExtra(数据);
    }
}
 

这里是preference.xml:

 < preferenceCategory
    机器人:关键=@字符串/ pref_category_myNote_key
    机器人:标题=@字符串/ pref_category_myNote>
    < com.flounder.xeniaNote.view.ImageView preference
        机器人:关键=@字符串/ pref_image_key
        机器人:标题=@字符串/ pref_image
        机器人:widgetLayout =@布局/ pref_account_image/>
< / preferenceCategory>
 

和呼叫 mImage pref.setImage 来改变你的的ImageView 在回调方法在preferenceClick

我希望它可以帮助!祝你好运。

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 ImageView in the layout using findViewById, but if I try to change the image this do not work.

Do I am wrong something?

PREFERENCE LAYOUT

 <?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>

CODE TO CHANGE THE IMAGEVIEW CONTENT

@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);

解决方案

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.

这篇关于如何管理自定义preference布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆