feagment可以实现多窗口,类似与html的frame,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局。
一,继承关系图
二,实例
1,运行效果
2,实现步骤
(1)界面布局
多窗口切换需要多个揭秘那布局,一个主布局和其他布局内容
maina_ctivity布局
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:background="#ffe545">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:gravity="center"
android:background="#f54565">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="doMessage"
android:background="@mipmap/message"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="doAddress"
android:background="@mipmap/addressbook"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="doFind"
android:background="@mipmap/find"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="doMe"
android:background="@mipmap/me"/>
</LinearLayout>
其他布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/find"/>
</LinearLayout>
(2)主界面Main
类似于打气筒的方式导入布局内容
public class FindActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_find,null);
}
}
在主界面中导入,按钮触发事件
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doMessage(View view){
MessageActivity ma = new MessageActivity();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content,ma);
ft.commit();
}
public void doAddress(View view){
AddressActivity aa = new AddressActivity();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content,aa);
ft.commit();
}
public void doFind(View view){
FindActivity fa = new FindActivity();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content,fa);
ft.commit();
}
public void doMe(View view){
MeActivity mea = new MeActivity();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content,mea);
ft.commit();
}
}