Android最佳方式动态更改片段中部分布局

Android最佳方式动态更改片段中部分布局,第1张

概述我有一个带有其布局的片段(fragment_layout.xml).在此布局中,我想对其图像中的一个部分(空布局)进行动态更改,以插入图像中所述的其他布局(第一,第二和第三布局).我不想更改片段的所有布局,而只需更改一部分.最好的方法是什么?解决方法:最好的方法是使用片段事务.检查此代码,在您

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

我不想更改片段的所有布局,而只需更改一部分.

最好的方法是什么?

解决方法:

最好的方法是使用片段事务.检查此代码,

在您的主要活动中,应将其扩展为FragmentActivity

@OverrIDepublic 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);    }}
总结

以上是内存溢出为你收集整理的Android最佳方式动态更改片段中部分布局全部内容,希望文章能够帮你解决Android最佳方式动态更改片段中部分布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存