2013-05-30 37 views
2

我有一個佈局片段(fragment_layout.xml)。在這個佈局中,我想要改變它的一個部分(空佈局)以插入其他部分佈局(第一,第二和第三佈局),如圖中所述。Android最好的方式動態地改變片段中的佈局部分

enter image description here

我不想改變片段的所有佈局,但只是其中的一部分。

這樣做的最佳方法是什麼?

回答

3

最好的方法是使用Fragment Transaction。檢查這個代碼,

在您的主要活動,應當擴大到FragmentActivity

@Override 
public void onClick(View button) { 
    // TODO Auto-generated method stub 
    FragmentTransaction ft=getActivity().getSupportFragmentManager().beginTransaction(); 

    if(button==groups)// If clicked button is groups, set the layout fragment1.xml 
    { 

     Fragment fragment = new GroupsFragment(); 

     FragmentManager fm = getActivity().getSupportFragmentManager(); 
     FragmentTransaction transaction = fm.beginTransaction(); 
     transaction.replace(R.id.fragment1, fragment); 
     transaction.commit(); 



    } 
    else if(button==photos) 
    { 

     Fragment fragment2 = new PhotosFragment(); 

     FragmentManager fm2 = getActivity().getSupportFragmentManager(); 
     FragmentTransaction transaction2 = fm2.beginTransaction(); 
     transaction2.replace(R.id.fragment1, fragment2); 
     transaction2.commit(); 





    } 







} 

而且在主佈局,

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

    <Button 
     android:id="@+id/button_profile_photos" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/relativeLayout3" 
     android:text="Photos" /> 

    <Button 
     android:id="@+id/button_profile_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button_profile_photos" 
     android:layout_alignBottom="@+id/button_profile_photos" 
     android:layout_toRightOf="@+id/button_profile_photos" 
     android:text="Groups" /> 

    <FrameLayout 
     android:id="@+id/fragment1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button_profile_photos" > 
    </FrameLayout> 

和組片段,

public class GroupsFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Inflating layout 
     View v = inflater.inflate(R.layout.groups_fragment, container, false); 
     // We obtain layout references 

     return v; 

    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     // Button reset=(Button)findViewById(R.id.reset); 

    } 

} 
+0

但我很高興y在片段中工作,廣告這是片段佈局。因此,即使您正在使用片段,我也只想更改片段佈局 – lory105

+0

的一部分,您可以使用上述方法更改片段中的部分。 –

+0

所以這成爲碎片的碎片?我可以使用相同的基本活動來實例化第二級片段的FragmentManager和FragmentTransaction? – lory105

相關問題