微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在适配器中使用android.r.id.content

如何解决在适配器中使用android.r.id.content

我做了以下代码,以便在我的应用中显示小吃店:

public void Pop_Snack(String text) {
    Snackbar snackbar = Snackbar.make( findViewById( android.R.id.content ),text,Snackbar.LENGTH_SHORT );
    View sbView = snackbar.getView();
    sbView.setBackgroundColor( getResources().getColor( R.color.colorPrimaryDark ) );

    TextView tv = (snackbar.getView()).findViewById( com.google.android.material.R.id.snackbar_text );
    Typeface font = ResourcesCompat.getFont( getBaseContext(),R.font.assistant );
    tv.setTypeface( font );
    tv.setTextSize( 14 );

    snackbar.setAnchorView( findViewById( R.id.bottom_navigation ) );

    snackbar.setDuration( 5000 );
    snackbar.show();
}

但是,在某些情况下,我想从适配器使用此snackbar。为了做到这一点,我尝试使用以下内容

public void Pop_Snack(String text,View v,Context context) {
    Snackbar snackbar = Snackbar.make( v.findViewById( android.R.id.content ),Snackbar.LENGTH_SHORT );
    View sbView = snackbar.getView();
    sbView.setBackgroundColor( context.getResources().getColor( R.color.colorPrimaryDark ) );

    TextView tv = (snackbar.getView()).findViewById( com.google.android.material.R.id.snackbar_text );
    Typeface font = ResourcesCompat.getFont( context,R.font.assistant );
    tv.setTypeface( font );
    tv.setTextSize( 14 );

    snackbar.setAnchorView( v.findViewById( R.id.bottom_navigation ) );

    snackbar.setDuration( 5000 );
    snackbar.show();
}

问题是我遇到了以下错误

java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.

还有其他方法可以让小吃店知道要显示哪个活动吗?

我尝试过类似的事情

Snackbar snackbar = Snackbar.make( v.findViewById(R.layout.activity_myitems),Snackbar.LENGTH_SHORT );

但我收到其他错误

谢谢

解决方案:

更改为:

Snackbar snackbar = Snackbar.make( v,Snackbar.LENGTH_SHORT );

解决了问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。