如何在首选项中添加波纹效果

问题描述 投票:0回答:1

我有一个PreferenceFragemnt,我希望对Preferences产生costum涟漪效应。所以我开始制作一个CostumPreference,但是我不知道如何设置背景可绘制。我找到了this post。但这对我不起作用。

有我的CostumPreference:

public class RipPreference extends Preference {
    private Context ctx;

    public RipPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        ctx = context;
    }

    public RipPreference(Context context) {
        super(context);
        ctx = context;
    }

    //This event won't be tiggered
    protected View onCreateView(ViewGroup parent) {
        //Preference is not an View so there is no onCreateView
        View view = super.onCreateView(parent);
        view.setBackground(<my ripple>);
        return view;
    }
}

    private void setCustomStyle(View view) {
        RippleDrawable drawable = (RippleDrawable) ctx.getDrawable(R.drawable.settings_preference_background);
        view.setBackground(drawable);
    }
}

也许有人有另一个主意。 :)

android android-preferences
1个回答
0
投票

您链接到他们的帖子中使用的是onBindView方法,而不是onCreateView

所以您可以尝试:

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    view.setBackground(<my ripple>);
}

编辑:androidx首选项库使用回收者视图,并且绑定视图的方法称为onBindViewHolder

@Override
protected void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    holder.itemView.setBackground(<my ripple>);
}
© www.soinside.com 2019 - 2024. All rights reserved.