Android:在PreferenceActivity中编辑自定义首选项视图的子项

时间:2012-07-14 20:01:26

标签: android android-preferences

因此,我为偏好活动中的一个偏好设置了自定义偏好设置视图:

<Preference 
      android:key="facebook_sharing"
      android:layout="@layout/fb_pref_item"/>

我希望能够以编程方式在该自定义首选项布局中编辑ImageView(基于首选项本身的值。

它看起来像这样:

Preference facebookItem = (Preference)findPreference("facebook_sharing");
facebookItem.setOnPreferenceChangeListener(this);

RelativeLayout rl = //get relative layout someway or another...

mFacebookIcon = (ImageView)rl.findViewById(R.id.fb_imgview);
if(!mFacebookConn.isSessionValid())
    mFacebookIcon.setColorFilter(R.color.transparent_black);

我无法弄清楚如何推出那条评论线。我尝试了以下内容:

RelativeLayout rl = (RelativeLayout)facebookItem.getView(null, null); //dont know what the params ought to be...

RelativeLayout rl = (RelativeLayout)findViewById(facebookItem.getLayoutResource());

都没有工作。我四处搜索,但似乎找不到任何试过这个的人。有任何想法吗?谢谢!

编辑:提供fb_pref_item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fb_view"
    android:layout_width="match_parent"
    android:layout_height="68dp" >

    <TextView android:id="@+id/fb_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="2dp"
        android:layout_alignParentTop="true"
        android:text="@string/facebook"
        android:textSize="20sp"
        android:textColor="@color/item_view_text_color" />

    <TextView android:id="@+id/fb_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="8dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/fb_title" />

    <ImageView android:id="@+id/fb_imgview"
        android:src="@drawable/facebook64"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_centerVertical="true"
        android:layout_margin="12dp"  
        android:layout_alignParentRight="true" 
        android:scaleType="centerInside"
        android:contentDescription="@string/cd_icon"/>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

根据目前为止提供的信息(请参阅上面的评论,我建议发布fb_pref_item.xml来源 - 您也没有说明您是否有多个ImageView,或只有一个),在我看来,你似乎并不是在寻找正确的观点。而不是:

RelativeLayout rl = //get relative layout someway or another...
mFacebookIcon = (ImageView)rl.findViewById(R.id.fb_imgview);

看起来您应该能够完全忽略这个不必要的RelativeLayout,然后直接转到fb_imgview

mFacebookIcon = (ImageView) this.findViewById(R.id.fb_imgview);

请注意,在这种情况下,您将使用继承的findViewById(int)方法,PreferenceActivity直接从Activity继承,而不是先尝试查找RelativeLayout

您无法使用Preference#getView()方法,因为它用于生成视图(然后将在PreferenceActivity中呈现)。 在视图已经渲染后获取