【问题标题】:How to change the activity's layout after pressing the camera capture button按下相机捕获按钮后如何更改活动的布局
【发布时间】:2013-01-07 13:35:41
【问题描述】:

我正在编写一个程序,该程序具有使用手机相机拍照的活动。 相机的 SurfaceView 下方有一个捕捉按钮。现在当用户按下捕获按钮时,我希望表面视图显示图片的预览,并且在此预览下应该有两个按钮(接受和取消)。如果用户按下取消,那么程序应该返回到相机预览以拍摄另一张照片。

我假设我应该为此定义两个布局,但我不知道如何在活动中从一个布局更改为另一个布局。

【问题讨论】:

    标签: android android-layout android-activity android-camera android-button


    【解决方案1】:

    有多种方法可以实现这一点。

    1. ViewGroup 中定义按钮,即 LinearLayoutvisibility="gone" 在 layout.xml 文件中。然后调用myButtons.setVisibility(View.VISIBLE); 使它们可见。要再次隐藏它们,请致电myButtons.setVisibility(View.GONE);

    2. 以编程方式创建按钮并在运行时将它们附加到 SurfaceView。

    我更喜欢选项 1。

    【讨论】:

      【解决方案2】:

      您可以为此使用两个片段。

      第一个片段有一个带有相机按钮的布局。 第二个片段有预览和两个按钮接受/取消。

      您可以使用 backstack,以便取消(或用户按回)将带您回到第一个片段。

      这是片段教程 (http://developer.android.com/guide/components/fragments.html) 中的代码,展示了如何用另一个片段替换片段

      // Create new fragment and transaction
      Fragment newFragment = new ExampleFragment();
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      
      // Replace whatever is in the fragment_container view with this fragment,
      // and add the transaction to the back stack
      transaction.replace(R.id.fragment_container, newFragment);
      transaction.addToBackStack(null);
      
      // Commit the transaction
      transaction.commit();
      

      您的取消按钮可以使用 popBackStack()(模拟用户的 Back 命令)从返回堆栈中弹出片段。

      还有另一种选择。如果您不需要预览的自定义布局,您可以跳过使用自定义摄像头,只需使用 startActivityForResult 将摄像头作为意图打开。 Intent 将允许用户拍照(在这种情况下,它会将生成的照片与您的主要活动进行通信),并且还有一个取消按钮,该按钮可以在不拍照的情况下返回到您的应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多