2014-06-11 41 views
0

當點擊frButtonFragmentOne應該打開並且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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context="com.megavb.le.FragmentOne" > 

<!-- TODO: Update blank fragment layout --> 

<TextView 
    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" /> 

<ToggleButton 
    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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="10dp" 
android:orientation="vertical" > 


<Button 
    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" /> 

</RelativeLayout> 

請參閱更好地瞭解圖片...

PICTURE HERE

+0

請張貼'R.layout.fragment_one' –

+0

R.佈局文件layout.fragment_one在主要消息中提交 – user3727505

+0

R.id.mainlayout是對FrameLayout的引用嗎? –

回答

3

因此,您應該使用ftr.replace而不是ftr.add

public void gofag1(View v) { 
    // TODO Auto-generated method stub 
    FragmentOne frag = new FragmentOne(); 
    FragmentManager fm = getFragmentManager(); // import 
    FragmentTransaction ftr = fm.beginTransaction(); // import 

    //Notice the change here 
    ftr.replace(R.id.mainlayout, frag, "keyFrag"); 
    ftr.addToBackStack(null); 
    ftr.commit(); 
} 

希望這有助於!

編輯:好吧,沒有更多的信息,我要採取另一個刺此。

在你的mainlayout.xml中,你需要定義一個Fragment Container,這樣做的一個簡單方法就是使用一個佈局文件,就像後面的佈局文件一樣。

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fragment_container"> 
</FrameLayout> 

定義這個新的片段容器後,你可以通過引用它的ID來編程添加片段。

下一步是將原始MainActivty代碼移動到新的片段中!

這是我們創建了一個新的佈局,僅僅包含一個按鈕fragment_main

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:onClick="gofag1" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

下面的代碼。

接下來,我們創建實際的片段

public class FragmentMain extends Fragment { 

    public FragmentMain() { 
     // 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_main, container, 
       false); 
} 

從這裏我們可以改變我們的主要業務OnCreate代碼

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainlayout); 
    FragmentMain frag = new FragmentMain(); 
    FragmentManager fm = getFragmentManager(); // import 
    FragmentTransaction ftr = fm.beginTransaction(); // import 
    ftr.add(R.id.fragment_container, frag, "keyFrag"); 
    ftr.addToBackStack(null); 
    ftr.commit(); 


} 
+0

試過...但仍然無法使用! :( – user3727505

+0

你可以添加你的'mainlayout.xml'的內容嗎? –

+0

肯定..我正在嘗試,但它不讓我在原始消息中添加更多代碼.. – user3727505