2013-06-21 123 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"