我已经使用 CoordinatorLayout 显示 Snackbar,但在某些布局中我没有使用 CoordinatorLayout 布局,我想显示 Snackbar,但遇到了问题。
我尝试了以下方法来在没有 CoordinatorLayout 的情况下实现 Snackbar,但它会显示以下结果。
Snackbar.make(getActivity().getWindow().getDecorView().getRootView(), "Testing Snackbar", Snackbar.LENGTH_LONG).show();
使用 android 8.0 Oreo 进行测试
有没有办法在没有CoordinatorLayout的情况下实现Snackbar?
提前致谢。
code
public class FirstFragment extends Fragment {
RelativeLayout rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
Log.e("onCreateView", "onCreateView");
rootView = (RelativeLayout)view.findViewById(R.id.rootView);
setSnackBar(rootView, "Testing with answered");
return view;
}
public static void setSnackBar(View coordinatorLayout, String snackTitle) {
Snackbar snackbar = Snackbar.make(coordinatorLayout, snackTitle, Snackbar.LENGTH_SHORT);
snackbar.show();
View view = snackbar.getView();
TextView txtv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
txtv.setGravity(Gravity.CENTER_HORIZONTAL);
}
}
尝试这段简单的代码。使用来自 android 框架本身的虚拟视图
Snackbar.make(findViewById(android.R.id.content),"Your Text Here",Snackbar.LENGTH_SHORT).show();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)