简体   繁体   English

在片段中创建浮动操作按钮

[英]Creating Floating Action Button in Fragment

I just wanted to build a floating action bar in one of the fragments but unfortunately, when I run the app it crashes.我只是想在其中一个片段中构建一个浮动操作栏,但不幸的是,当我运行该应用程序时它崩溃了。

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); add_button =(FloatingActionButton)getView().findViewById(R.id.add_button); add_giftcard =(FloatingActionButton)getView().findViewById(R.id.add_giftcard); add_flight =(FloatingActionButton)getView().findViewById(R.id.add_flight); //animations fabOpen = AnimationUtils.loadAnimation( getActivity(), R.anim.fab_open); fabClose = AnimationUtils.loadAnimation( getActivity(), R.anim.fab_close); rotateForward = AnimationUtils.loadAnimation( getActivity(), R.anim.rotate_forward); rotateBackward = AnimationUtils.loadAnimation( getActivity(), R.anim.rotate_backward);

According to the fragment lifecycle, the view should be initialized within the onCreateView() callback.根据片段生命周期,视图应在 onCreateView() 回调中初始化。 Use the below snippet.使用以下代码段。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.your_fragment_view, container, false);

add_button =(FloatingActionButton)view.findViewById(R.id.add_button);
        add_giftcard =(FloatingActionButton)view.findViewById(R.id.add_giftcard);
        add_flight =(FloatingActionButton)view.findViewById(R.id.add_flight);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM