Java Android-测试嵌套片段
如果我做得正确,我将尝试创建一个嵌套的片段,但没有相对的进展。第一个onCreateView包含它的视图,但在onCreateView的旁边,我实例化了一个新类,该类扩展了一个片段,并且还拥有自己的onCreateView和xml。我现在有错误了。我只想在第一个主视图中显示Java Android-测试嵌套片段,java,android,android-fragments,Java,Android,Android Fragments,如果我做得正确,我将尝试创建一个嵌套的片段,但没有相对的进展。第一个onCreateView包含它的视图,但在onCreateView的旁边,我实例化了一个新类,该类扩展了一个片段,并且还拥有自己的onCreateView和xml。我现在有错误了。我只想在第一个主视图中显示VideoPlayerFragment。希望了解更多 public static class DemoObjectFragment extends Fragment { public static final Str
VideoPlayerFragment
。希望了解更多
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_main_menu_activity, container, false);
//Bundle args = getArguments();
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.testLayout, videoFragment).commit();
//((TextView) rootView.findViewById(android.R.id.text1)).setText(
// Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
VideoPlayerFragment
公共类VideoPlayerFragment扩展了片段{
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.id.testLayout, container, false);
Bundle args = getArguments();
//((TextView) rootView.findViewById(android.R.id.text1)).setText(
// Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
测试\u片段
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.jinisys.restoplusordering.VideoPlayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lommeregnerv2">
<LinearLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
</fragment>
再次感谢@Luksprog的提示,我自己就知道了。因为我想要一个嵌套片段,所以我需要一个父xml来添加下面的代码。这段代码使我能够在片段中添加另一个片段
<FrameLayout
android:id="@+id/fragg"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
那个布局文件是怎么回事?不能让片段标记包装其他元素。还有,你在哪里使用这个布局文件?这个布局只是用来测试的,我只是在试验这是否可行,但不知道我是否做得对。清理你的项目。另外,LayoutInflater
的inflate()
方法需要一个R.layout形式的资源。layout
(一个布局文件引用)而不是像您使用的R.id.testLayout
。看起来容器有问题,您正在添加片段,请检查,无论您是否正确指定了容器id,即正在膨胀的布局文件具有指定的viewgroup容器,我都必须在load\u main\u menu\u activity
xml中放置一个FrameLayout
,并删除testing\u fragments
-此xml会造成混乱。
<FrameLayout
android:id="@+id/fragg"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent" />