gpt4 book ai didi

android - 使用自定义布局获取偏好 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:44 25 4
gpt4 key购买 nike

我有一个偏好 android:layout gft。我已经在 xml 文件中首选项的 android:layout 属性中设置了布局。

现在我需要对布局中的按钮进行初始化(这是一个 +1 按钮)。我需要在 onCreate 方法中获取该按钮(它是一个 +1 按钮),但是当我使用 findViewById(button_id) 时,它返回 null。

有什么方法可以获取此偏好的 View 吗?

更新:似乎在我添加首选项 xml 后没有创建首选项,所以我得到 null。我在哪里可以调用 findViewById 以便已经创建按钮?

谢谢。

首选项 xml:

<Preference android:title="Rate this app"
android:key="rate"
android:widgetLayout="@layout/plusone_pref"/>

</PreferenceScreen>

首选项布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.plus.PlusOneButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="@+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
plus:size="standard" />

最佳答案

您需要创建一个扩展Preference 的类,然后重写onBindView,您就可以访问该 View 。

public class MyPreference extends Preference {

...

@Override
protected void onBindView(View view) {
super.onBindView(view);
View myView = (View) view.findViewById(R.id.my_id);
// ... do stuff
}

}

您可能需要覆盖更多方法,阅读 PreferenceActivity 中的自定义首选项 - http://developer.android.com/guide/topics/ui/settings.html

关于android - 使用自定义布局获取偏好 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233440/

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