android - 如何管理自定义首选项布局

标签 android android-layout

我有一个图像和两个文本的自定义首选项,我需要以编程方式更改图像。

我能够获取自定义首选项 View 并使用 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);

最佳答案

我尝试了 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在回调方法onPreferenceClick中改变你的ImageView

希望对您有所帮助!祝你好运。

关于android - 如何管理自定义首选项布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11790163/

相关文章:

android - ListView 之间的按钮消失

android - 可重用 View 组

android - 将 View 粘贴到 ScrollView 的底部

java - 错误 : NullPointerAccess: The variable "" can only be null at this location

java - 删除 XML 组件而不是隐藏?

java - 无法编写基于每个项目保存的长期过滤掉 RecyclerView 中的项目的算法

java - Android,我应该将内容居中吗?

android - 我如何在 Android 的 SharedPreferences 中存储一个整数数组?

java - 如何在 Eclipse 中的 Java 构建路径中更新 Android

android - ScrollView 无法正确调整自身大小