Custom preference not clickable

2020-06-27 10:57发布

问题:

I added a custom preference to my project (code below). I added it to my preferences xml with a custom widgetLayout:

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

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

I see the layout and the button in the layout works fine. The only problem is that the preference isn't clickable. Like it's hidden behind something.

Any ideas on how to make it clickable?

If I add a regular Preference (without a widget layout) it works fine.

Thanks.

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);
}
  }

回答1:

in layout/plusone_pref.xml set android:focusable="false" for your Button



回答2:

Putting Pskink's answer together with Ran's comment:

  • If your custom preference's layout is a ViewGroup (e.g. a *Layout), use android:descendantFocusability="blocksDescendants"
  • If it's just one View, use android:focusable="false"


回答3:

Preferences don't have a clickable attribute, though there is an onClick() method. Tryandroid:selectable.