7

是否可以以编程方式访问设置为首选项的布局?

这是我所拥有的,一个非常简单的项目 - 概念验证

偏好活动:

package com.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;

public class PreferenceExampleActivity extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        ImageView v = (ImageView) findViewById(R.id.iconka);

    }
}

资源 XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:key="settings">
    <PreferenceCategory 
        android:title="Category Setting Name" 
        android:order="1" 
        android:key="Main">
        <Preference 
            android:order="1" 
            android:title="Setting" 
            android:summary="Setting1" 
            android:layout="@layout/profile_preference_row"
            android:key="profile" />
    </PreferenceCategory>
</PreferenceScreen>

首选项的自定义布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>
    <ImageView
        android:id="@+id/iconka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

我想要的是能够从 Activity 访问“iconka”ImageView 并从那里更改图像。我正在使用 API 8 (Android 2.2)

目前“v”为空,我不知道为什么会这样。

一个提示将不胜感激!

更新 - 解决方案: 实际上,我需要一个自定义首选项,我可以根据自己的需要进行修改。这是一份如何在项目中创建自己的自定义首选项的实用指南: Android & Amir - Android Preferences 请参阅作者创建自定义首选项类时的部分。

4

3 回答 3

9

拉头发 20 分钟后,我找到了一个优雅的解决方案。首先,扩展首选项,然后覆盖 getView(View convertView, ViewGroup parent) 方法。我的情况是这样的:我有一个带有应用程序图标和 2 个文本视图(应用程序名称和版本)的偏好布局。我想以编程方式更改应用程序版本。我该怎么做呢?看看下面:

public class AboutUsPreference extends Preference {

    public AboutUsPreference(Context context) {
        super(context);
    }

    public AboutUsPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AboutUsPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        View v = super.getView(convertView, parent);
        ((TextView)v.findViewById(R.id.textView2)).setText(getAppVersion());        
        return v;
    }

    private String getAppVersion(){
        PackageInfo pInfo = null;
        try {
            pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
        } catch (NameNotFoundException e) {
            Log.e(getClass().getName(), e.getMessage(), e);
            return "";
        }

        String version = pInfo.versionName;
        return getContext().getString(R.string.version, version);
    }


}

解决方案是这样的:View v = super.getView(convertView, parent);覆盖 getView 方法时。super.getview 调用将返回您的布局视图。

我的 xml 偏好如下所示:

<com.audioRec.android.settings.aboutUs.AboutUsPreference
        android:layout="@layout/about_preference_layout"/>
于 2013-08-22T15:32:02.603 回答
1

尝试getLayoutResource获取View首选项,然后获取您的ImageView

于 2011-11-15T22:34:32.810 回答
1

试着看看下面关于 imageview 的讨论。可能会帮助您解决您的问题。

Android:ImageView 的 findViewById(自定义适配器)

Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException

于 2011-11-15T23:40:08.550 回答