Android在片段中动态更改部分布局的最佳方法

Posted

技术标签:

【中文标题】Android在片段中动态更改部分布局的最佳方法【英文标题】:Android best way to change dynamically part of layout in fragment 【发布时间】:2013-05-25 22:25:10 【问题描述】:

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

我不想更改片段的所有布局,只更改其中的一部分。

最好的方法是什么?

【问题讨论】:

【参考方案1】:

最好的方法是使用 Fragment Transaction。检查此代码,

在您的 Main Activity 中,应该扩展到 FragmentActivity

 @Override
    public void onClick(View button) 
        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_
android:layout_
tools:context=".ProfileActivity" >

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

    <Button
        android:id="@+id/button_profile_group"
        android:layout_
        android:layout_
        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_
        android:layout_
        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);

    


【讨论】:

但是我已经在片段中工作了,这就是片段布局。所以我只想改变片段布局的一部分 即使您使用的是片段,您也可以使用上述方法更改片段中的部分。 所以这变成了碎片的碎片?并且我可以使用相同的基础活动来为二级片段实例化 FragmentManager 和 FragmentTransaction? 是的,新的 android 支持库现在支持嵌套片段。因此您可以在另一个片段中实现一个片段。【参考方案2】:

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

【讨论】:

以上是关于Android在片段中动态更改部分布局的最佳方法的主要内容,如果未能解决你的问题,请参考以下文章

Android片段中的动态背景

Android片段布局完成膨胀

在Android上为Constraint布局更改设置动画的最佳方式

片段中的按钮自定义视图

避免android片段中内存泄漏的最佳方法是啥

更改片段中按钮的文本