相机与导航抽屉重叠 - Android

Posted

技术标签:

【中文标题】相机与导航抽屉重叠 - Android【英文标题】:Camera is overlapping the Navigation Drawer - Android 【发布时间】:2017-07-08 02:42:41 【问题描述】:

我再次需要帮助,伙计们。看来我的相机活动(这是一个片段)与我的导航抽屉重叠。我不知道如何解决这个问题。

这是我的样子:

这是我的 CameraFragment.java 代码:

public class CameraFragment extends Fragment 

    private Camera mCamera = null;
    private CameraView mCameraView = null;


    View v;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        v = inflater.inflate(R.layout.activity_first_fragment, container, false);


        try 
            mCamera = Camera.open();//you can use open(int) to use different cameras
         catch (Exception e) 
            Log.d("ERROR", "Failed to get camera: " + e.getMessage());
        

        if (mCamera != null) 
            mCameraView = new CameraView(getActivity(), mCamera);//create a SurfaceView to show camera data
            FrameLayout camera_view = (FrameLayout) v.findViewById(R.id.camera_view);
            camera_view.addView(mCameraView);//add the SurfaceView to the layout
            //  camera_view.onCreate(savedInstanceState);
        
        //-------------------------------------------------------------------------------camera

        return v;
    

    private class CameraView extends SurfaceView implements SurfaceHolder.Callback 

        private SurfaceHolder mHolder;
        private Camera mCamera;

        public CameraView(Context context, Camera camera) 
            super(context);
            mCamera = camera;
            mCamera.setDisplayOrientation(90);
            //get the holder and set this class as the callback, so we can get camera data here
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        

        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) 
            try
                //when the surface is created, we can set the camera to draw images in this surfaceholder
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.startPreview();
             catch (IOException e) 
                Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
            
        

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) 
            //before changing the application orientation, you need to stop the preview, rotate and then start it again
            if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
                return;

            try
                mCamera.stopPreview();
             catch (Exception e)
                //this will happen when you are trying the camera if it's not running
            

            //now, recreate the camera preview
            try
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
             catch (IOException e) 
                Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
            
        

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) 
            //our app has only one screen, so we'll destroy the camera in the surface
            //if you are using with more screens, please move this code your activity
            mCamera.stopPreview();
            mCamera.release();
        
        
    

这是我的 activity_camera.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_first_fragment"
    android:layout_
    android:layout_
    tools:context="com.dcar.FirstFragment">
<!--
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second_fragment"
    android:layout_
    android:layout_
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.dcar.MainActivity"
    >
-->
    <FrameLayout
        android:id="@+id/camera_view"
        android:layout_
        android:layout_
        >

    </FrameLayout>
<!--
</FrameLayout>
-->
</RelativeLayout>

MainActivity.java:

 @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();



     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

Fragment home = new FirstFragment();
        FragmentManager FM = getFragmentManager();
        FM
                .beginTransaction()
                .replace(R.id.content_frame, home)


                  .commit();


@Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        android.app.FragmentManager fragmentManager = getFragmentManager();

        if (id == R.id.action_ar) 
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, new CameraFragment())
                    .commit();
        
        else if (id == R.id.action_map) 
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, new SecondFragment())
                    .commit();

        
        else if (id == R.id.action_direction) 

            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, new ThirdFragment())
                    .commit();

        

我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_
    android:layout_
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_
        android:layout_ />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

什么导航抽屉?在通常意义上的“抽屉”中,您的布局中没有任何内容可以创建抽屉。 我有。但我没有把它贴在这里。正如您在“单击此处”中看到的那样,我将名称导航抽屉(蓝色)以便您识别它。 如果你有一个涉及抽屉的问题,我们需要看看抽屉是如何实现的。仅仅一张图片是不够的。您还应该在Activity 中包含您发生的任何Fragment 交易。 好的,先生。顺便说一句,完成编辑。 =) 嗯,一切似乎都很好,假设 content_frameapp_bar_main 内。 Fragments 的其余部分是否按预期工作?另外,您不使用支持Fragments 的任何特殊原因? 【参考方案1】:

我有同样的问题。这对我有用

drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() 
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) 
        drawerLayout.bringChildToFront(drawerView);
        drawerLayout.requestLayout();
    
    ...

【讨论】:

以上是关于相机与导航抽屉重叠 - Android的主要内容,如果未能解决你的问题,请参考以下文章

Lollipop导航栏重叠Dialog与Gravity.Bottom

在 Android 中创建带有导航抽屉的汉堡菜单

导航抽屉活动:在按钮单击时从片段移动到片段

Lollipop 导航栏与 Gravity.Bottom 重叠对话框

如何将背景颜色设置为透明状态栏

导航抽屉(Google+ 与 YouTube)