A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

feagment可以实现多窗口,类似与html的frame,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局。

实例

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();
    }
}
---------------------
转载,仅作分享,侵删
作者:韩朝飞
原文:https://blog.csdn.net/weixin_41858200/article/details/85562220


1 个回复

倒序浏览
奈斯,加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马