我正在使用Commonsware创建的库来制作一个简单的相机应用程序.在我的自述中,有一段说明了这一点:
You can subclass CameraFragment and override onCreateView(). Chain to
the superclass to get the CameraFragment’s own UI, then wrap that in
your own container with additional widgets, and return the combined UI
from your onCreateView().
我对Android Fragments仍然不太满意,所以我希望有人可以向我解释这一点.
我有两个活动(A,B),一个片段(CamFragment)和两个布局(A,B).我的第一个活动(A)加载一个具有单个按钮和imageView的布局(A).该按钮将启动第二个活动(B),该活动将使用第二个布局(B)加载片段(CamFragment).第二个布局来自演示应用程序:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"/>
因此,现在在我的应用程序中,我希望在摄影机片段/第二个活动上有一个按钮,它将takePicture()onClick.按照自述文件中的说明,我需要首先对CameraFragment进行子类化.这很容易,我只是这样做:
public class CamFragment extends CameraFragment {
接下来,我必须重写onCreateView.同样,这项琐碎的任务就像v9演示中一样.
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
setHost(new DemoCameraHost(getActivity()));
}
下一部分是我真正感到困惑的地方,希望有人能提供帮助.
Chain to the superclass to get the CameraFragment’s own UI, then wrap
that in your own container with additional widgets, and return the
combined UI from your onCreateView().
我只是不完全确定这意味着什么.如果有一个演示,它展示了一个没有actionBar的自定义相机UI,那将是非常棒的,因为我觉得大多数开发人员都不会将控件放到Action Bar中.
编辑#1:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View uiFromParent=super.onCreateView(inflater, container, savedInstanceState);
View yourUi = inflater.inflate(R.layout.main_cam, container, false);
View theThingThatWillHoldThePreview = yourUi.findViewById(R.id.holder); // or whatever
((ViewGroup) theThingThatWillHoldThePreview).addView(uiFromParent); // with some appropriate LayoutParams
return super.onCreateView(inflater, container, savedInstanceState);
}
main_cam.xml
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="@color/abs__bright_foreground_holo_dark"
android:keepScreenOn="true"
tools:ignore="MergeRootFrame" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="@color/abs__bright_foreground_holo_dark"
android:src="@drawable/ic_launcher"
android:text="Capture" />
cam_fragment.xml
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame"/>
解决方法:
Next, I have to override onCreateView. Again, a trivial task just like in the v9 demo.
您的代码未覆盖onCreateView().它覆盖onCreate().
I’m just not entirely sure what that means/entails.
“绑定到超类以获得CameraFragment自己的UI”
View uiFromParent=super.onCreateView(inflater, container, savedInstanceState);
“然后用其他小部件将其包装在您自己的容器中”
View yourUi=... // inflate something, create straight in Java, whatever
View theThingThatWillHoldThePreview=yourUi.findViewById(...); // or whatever
theThingThatWillHoldThePreview.addView(uiFromParent, ...); // with some appropriate LayoutParams
“并从onCreateView()返回组合的UI”
return(yourUi);
标签:android-camera,commonsware-cwac,java,android
来源: https://codeday.me/bug/20191122/2062108.html