获取鉴于preference与自定义布局 [英] Get view of preference with custom layout

查看:231
本文介绍了获取鉴于preference与自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个preference的Andr​​oid:布局GFT。 我给自己定在Android的布局:的preference在XML文件格式属性

现在我需要做一个按钮的初始布局(这是一个+1按钮)。 我需要在onCreate方法的按钮(这是一个+1按钮),但是当我使用 findViewById(button_id),则返回null。

有没有办法让这个preference的看法?

更新:这似乎是preference没有创建后我添加了preference XML的,所以我得到空。我在哪里可以打电话findViewById因此已创建的按钮?

感谢。

preferences的xml:

 < preference机器人:标题=评分程序
    机器人:关键=率
    机器人:widgetLayout =@布局/ plusone_ preF/>

< / preferenceScreen>
 

preference布局的xml:

 < XML版本=1.0编码=UTF-8&GT?;
< com.google.android.gms.plus.PlusOneButton的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:加=htt​​p://schemas.android.com/apk/lib/com.google.android.gms.plus
机器人:ID =@ + ID / plus_one_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_vertical
机器人:可聚焦=假
加:大小=标准/>
 

解决方案

您需要创建一个扩展的类 preference ,然后重写 onBindView ,你将有机会获得的观点。

 公共类我的preference扩展preference {

    ...

    @覆盖
    保护无效onBindView(查看视图){
        super.onBindView(视图);
        查看MyView的=(查看)view.findViewById(R.id.my_id);
        // ...做的东西
    }

}
 

您可能会需要重写一些方法,以preferenceActivity了解定制preference - http://developer.android.com/guide/topics/ui/settings.html

I have a preference android:layout gft. I've set the layout in the android:layout property of the preference in the xml file.

Now I need to do an initialization of a button in the layout (it's a +1 button). I need to get that button in the onCreate method (it's a +1 button) but when I use findViewById(button_id) , it returns null.

Is there some way to get the view of this preference?

UPDATE: It seems like the preference isn't created after I add the preference xml, so I get null. Where I can call findViewById so the button is already created?

Thanks.

Preferences xml:

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

</PreferenceScreen>

Preference layout 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" />

解决方案

You need to create a class that extends Preference, then override onBindView and you'll have access to the 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
    }

}

You'll probably have to override some more methods, read about custom preference in PreferenceActivity - http://developer.android.com/guide/topics/ui/settings.html

这篇关于获取鉴于preference与自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆