【问题标题】:Picture overlaps the Navigation Drawer图片与导航抽屉重叠
【发布时间】:2015-12-21 13:54:15
【问题描述】:

我在前置摄像头预览(片段中)上有图片(一顶帽子),因此您可以在购买前试戴帽子。但问题是,当在此预览中我试图打开导航抽屉时,我的帽子与此抽屉重叠,前置摄像头位于导航抽屉下方。如何使图片(帽子)位于导航抽屉下方?

MirrorFragment.java:

public class MirrorFragment extends Fragment implements SurfaceHolder.Callback {

    private Camera camera = null;

    private SurfaceHolder cameraSurfaceHolder = null;
    private boolean previewing = false;
    private static int wid = 0, hgt = 0;
    private View cameraViewControl;
    private Button btnCapture;
    private Activity activity;

    public MirrorFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (Activity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        SurfaceView cameraSurfaceView;
        Display display;


        View view = inflater.inflate(R.layout.fragment_mirror, container, false);
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        int toolbarHeight = ((MainActivity) activity).getToolbar().getHeight();


        display = getActivity().getWindowManager().getDefaultDisplay();

        wid = display.getWidth();
        hgt = display.getHeight();


        activity.getWindow().setFormat(PixelFormat.TRANSLUCENT);
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        cameraSurfaceView = (SurfaceView) view.findViewById(R.id.cameraSurfaceViewFragment);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();

        cameraSurfaceHolder.addCallback(this);

        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

        RelativeLayout.LayoutParams layoutParamsControl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);

        btnCapture = (Button) cameraViewControl.findViewById(R.id.btnCapture);

        setMarginTopForHat(toolbarHeight);


        activity.addContentView(cameraViewControl, layoutParamsControl);

        return view;
    }




    public void setMarginTopForHat(int height) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnCapture.getLayoutParams();
        layoutParams.setMargins(0, height + 30, 0, 0);
        btnCapture.setLayoutParams(layoutParams);
    }    


    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            camera = Camera.open(1);
            camera.setDisplayOrientation(90);
        } catch (RuntimeException e) {
            Toast.makeText(getContext(), "Device camera is not working properly, please try after sometime.",
                    Toast.LENGTH_LONG).show();
        }
    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        ((ViewGroup) cameraViewControl.getParent()).removeView(cameraViewControl);
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}

fragment_mirror.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <SurfaceView
        android:id="@+id/cameraSurfaceViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

【问题讨论】:

    标签: android android-fragments android-camera navigation-drawer


    【解决方案1】:

    您的帽子View 出现在抽屉上方,因为您正在使用活动的addContentView() 方法,该方法将其添加为活动内容ViewGroup 中的最后一个子项,使其绘制在其余部分之上它的内容。如果您将MirrorFragment 加载到其中的ViewGroupFrameLayout,则可以将其添加到其中。但是,可能更可取的解决方案是将其直接添加到 fragment_mirror 布局中,可以通过编程方式,也可以在布局 XML 中。

    【讨论】:

    • 抱歉,没听懂。我应该将方法addContentView() 更改为其他方法吗?因为我试图在addContentView() 中输入不同的参数(例如View view = inflater.inflate(R.layout.fragment_mirror, container, false); 并且它不起作用。
    • 你不想使用addContentView(),因为这就是导致帽子视图出现在其他所有东西上的原因。您想使用您决定将帽子视图添加到的任何 ViewGroup 的 addView() 方法。我建议将其直接添加到view,然后再将其返回到onCreateView()。不过,您可能需要更改 fragment_mirror 布局,具体取决于它作为根的 ViewGroup 类型。如果您想发布该布局,我可以更具体。
    • 我编辑了我的问题并发布了 MirrorFragment 的布局。请看一看。
    • 好的,看起来您已经准备好了。只需将activity.addContentView() 行更改为((RelativeLayout) view).addView(cameraViewControl, layoutParamsControl);。 (我假设你的帽子视图在cameraViewControl。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2021-07-26
    • 2023-04-03
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多