当点击frButton时FragmentOne应该打开并且MainActivity应该隐藏,但MainActivity的组件仍然可以在FragmentOne中访问,这是不需要的。 如何解决它?为MainActivityAndroid:片段
代码:为FragmentOne
public void gofag1(View v) {
// TODO Auto-generated method stub
FragmentOne frag = new FragmentOne();
FragmentManager fm = getFragmentManager(); // import
FragmentTransaction ftr = fm.beginTransaction(); // import
ftr.add(R.id.mainlayout, frag, "keyFrag");
ftr.addToBackStack(null);
ftr.commit();
}
代码:为FragmentOne布局
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_one, container,
false);
return v;
}
}
代码... R.layout.fragment_one
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.megavb.le.FragmentOne" >
android:id="@+id/tOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="@string/a1" />
android:id="@+id/toggleButton1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="reedToggleValue"
android:text="ToggleButton"
android:textSize="30sp" /> 为MainActivity布局
代码.... R.id.mainlayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="gofag1"
android:text="fOne" />
请参阅更好地了解图片...
+0
请张贴'R.layout.fragment_one' –
+0
R.布局文件layout.fragment_one在主要消息中提交 –
+0
R.id.mainlayout是对FrameLayout的引用吗? –