以编程方式向片段布局添加自定义视图

时间:2015-12-18 13:55:38

标签: android android-fragments

我有一个View类,我想添加到我的Fragment中,但我无法弄清楚如何去做。

这是我的片段:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view =  inflater.inflate(R.layout.fragment_pong, container, true);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RelativeLayout pongField = (RelativeLayout) getActivity().findViewById(R.id.pongGameField);
        pongField.addView(new SingleTouchEventView(getActivity(), null));
    }

我尝试将SingleTouchEventView添加到片段的onViewCreated方法中的片段,但它不起作用。

这是错误:

12-18 14:49:11.479 18177-18177/com.example.pymdev.pym E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                            at android.view.ViewGroup.addViewInner(ViewGroup.java:3439)
                                                                            at android.view.ViewGroup.addView(ViewGroup.java:3310)
                                                                            at android.view.ViewGroup.addView(ViewGroup.java:3255)
                                                                            at android.view.ViewGroup.addView(ViewGroup.java:3231)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
                                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                            at android.os.Handler.handleCallback(Handler.java:615)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                            at android.os.Looper.loop(Looper.java:137)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:4960)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:511)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                                                                            at dalvik.system.NativeStart.main(Native Method)
12-18 14:49:34.129 18177-18177/com.example.pymdev.pym I/Process: Sending signal. PID: 18177 SIG: 9

请帮助,如何正确地将我的视图添加到片段布局?

2 个答案:

答案 0 :(得分:3)

  

一个布尔值,指示在通货膨胀期间是否应将膨胀的布局附加到ViewGroup(第二个参数)。

     

(在这种情况下,这是错误的,因为系统已经将膨胀的布局插入到容器中 - 传递true将在最终布局中创建冗余视图组。)

有关详细信息,请参阅to the documentation

使用此

View view=inflater.inflate(R.layout.fragment_pong, container,false);

而不是

View view=inflater.inflate(R.layout.fragment_pong, container,true);

答案 1 :(得分:-1)

试试这个

.dropdown-backdrop {
  display: none;
}