gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:17:24 24 4
gpt4 key购买 nike

所以我为我的偏好 Activity 中的一个偏好构建了一个自定义偏好 View :

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

最佳答案

根据目前提供的信息(参见上面的评论,我建议在此处发布 fb_pref_item.xml 源代码——您也没有说明您是否有多个 ImageViews,或者只是一个),在我看来你不是在寻找正确的观点。而不是这个:

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() 方法,因为它用于生成 View (然后将在 PreferenceActivity).在 View 已经呈现后,它不用于获取 View 。

关于Android:在 PreferenceActivity 中编辑自定义首选项 View 的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com