Android碎片兼顾平板简单示例
程序员文章站
2022-03-23 19:44:40
示例一(简单):
activity_main:
left_fragment:
right_fragment:...
示例一(简单):
activity_main:
left_fragment:
right_fragment:
leftfragment:
package com.example.mjon.myapplication; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class leftfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.left_fragment,container,false); return view; } }
rightfragment:
package com.example.mjon.myapplication; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class rightfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.right_fragment,container,false); return view; } }
示例二(动态加载):
activity_main:
<framelayout android:id="@+id/right_fragment" android:layout_height="match_parent" android:layout_weight="1" android:layout_width="0dp"> </framelayout>
left_fragment:
right_fragment:
other_fragment:
leftfragment:
package com.example.mjon.myapplication; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class leftfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.left_fragment,container,false); return view; } }
rightfragment:
package com.example.mjon.myapplication; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class rightfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.right_fragment,container,false); return view; } }
otherfragment:
package com.example.mjon.myapplication; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class otherfragment extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = inflater.inflate(r.layout.other_fragment,container,false); return view; } }mainactivity:
package com.example.mjon.myapplication; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); replacefragment(new rightfragment()); button btn = (button)findviewbyid(r.id.button); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { replacefragment(new otherfragment()); } }); } private void replacefragment(fragment fragment){ fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.replace(r.id.right_fragment,fragment); transaction.commit(); } }