2013-06-21 124 views
2

我为我的项目添加了自定义首选项(代码如下)。我把它添加到我的喜好XML使用自定义widgetLayout:自定义首选项不可点击

<w.PlusOnePreference 
    android:title="Rate App" 
    android:key="custom" 
    android:widgetLayout="@layout/plusone_pref"/> 

偏好布局的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" /> 

我看到的布局,并在布局按钮正常工作。唯一的问题是,偏好不可点击。就像它隐藏在某物后面一样。

有关如何使其可点击的任何想法?

如果我添加一个常规首选项(没有小部件布局),它工作正常。

谢谢。

public class PlusOnePreference extends Preference { 

private PlusClient mPlusClient = null; 

public PlusOnePreference(Context context) { 
    super(context); 

} 


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

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

public void setPlusClient(PlusClient plusClient) { 
    mPlusClient = plusClient; 
} 



@Override 
protected void onBindView(View view) { 
    super.onBindView(view);  
    //mPlusOneButton = 
    PlusOneButton plusOneButton = (PlusOneButton)view.findViewById(R.id.plus_one_button); 
    plusOneButton.initialize(mPlusClient, SettingsActivity.URL, SettingsActivity.PLUS_ONE_REQUEST_CODE); 
} 
    } 
+0

我想你的代码贝科使用我正在尝试为我的动态壁纸添加一个加号按钮,但它表示我的首选项不是textview。我只是复制你的代码来尝试它。你有没有机会举出一个你可以分享给那些试图做同样事情的工作例子?谢谢。 –

+0

Preference类应该用新的google play服务更新,但布局是一样的。可能会用你的代码启动另一个问题,首选项不应该是TextView ...... – Ran

+0

没错。这是我的观点。我的代码和上面的代码完全一样。 Verbatim –

回答

8

/plusone_pref.xml设置机器人:可调焦=“假”为您的按钮

+0

它没有帮助。我添加了布局的xml。 – Ran

+0

OK,这是因为你的按钮实际上是一个ViewGroup,然后setFocusable(假)的所有儿童 – pskink

+1

或用这个顶层容器(没有测试它虽然)安卓 descendantFocusability =“ blocksDescendants” – pskink

0

首选项没有可点击的属性,尽管有onClick()方法。试试android:selectable

布局
+0

'androird:selectable = true'没有帮助。如果我在xml中放置了一个'Preference',那么它是可点击的,我在'onPreferenceTreeClick'中得到一个事件。 – Ran

+0

很高兴知道!谢谢。 – HannahRose

4

把Pskink的答案与阿兰的评论在一起:

  • 如果您的自定义偏好的布局是一个ViewGroup(如*布局),使用android:descendantFocusability="blocksDescendants"
  • 如果它只是一个View,使用android:focusable="false"