Android最好的方式来动态更改片段中的部分布局

时间:2013-05-30 11:07:53

标签: android layout fragment

我有一个带有布局的片段(fragment_layout.xml)。在这个布局中,我想改变其中一个部分(空布局)以插入其他部分布局(第一,第二和第三布局),如图中所述。

enter image description here

我不想改变片段的所有布局,只想改变它的一部分。

最好的方法是什么?

2 个答案:

答案 0 :(得分: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);

    }

}

答案 1 :(得分:0)

第一个答案在技术上是正确的,但它将迫使您为这些部分中的每一个创建不同的片段类。如果它们中存在某些逻辑,则需要以某种方式将这些片段连接到父片段/活动,这很烦人。我会坚持另一种解决方案 - 将布局的一部分添加到现有布局中。 看到这个答案 How to add views dynamically to a RelativeLayout already declared in the xml layout?

相关问题