Android基础——Fragment的创建和添加

Android基础——Fragment的创建和添加,第1张

概述新建一个Fragment的过程packagecom.example.myactivityiiii;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;import

新建一个Fragment的过程

package com.example.myactivityiiii;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androIDx.annotation.NonNull;import androIDx.annotation.Nullable;import androIDx.fragment.app.Fragment;/** 新建一个Fragment的过程*   1.自定义一个类,基础Fragment*   2.配置该类的layout文件*   3.重写onCreateVIEw方法,加载该类的布局文件,返回一个VIEw* */public class ListFragment extends Fragment {    @Nullable    @OverrIDe    public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) {        VIEw vIEw = inflater.inflate(                R.layout.fragment_List,container,false        );        return vIEw;    }}
package com.example.myactivityiiii;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androIDx.annotation.NonNull;import androIDx.annotation.Nullable;import androIDx.fragment.app.Fragment;import java.util.zip.Inflater;public class DetailFragment extends Fragment {    @Nullable    @OverrIDe    public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) {        VIEw vIEw = inflater.inflate(                R.layout.fragment_detail,container,false        );        return vIEw;    }}

在Activity中添加Fragment

1.直接通过layout添加:在Main的布局文件中添加以下代码

<fragment    androID:layout_wIDth="wrap_content"    androID:layout_height="match_parent"    androID:name="com.example.myactivityiiii.ListFragment"    androID:ID="@+ID/List"    androID:layout_weight="1"/><fragment    androID:layout_wIDth="wrap_content"    androID:layout_height="match_parent"    androID:name="com.example.myactivityiiii.DetailFragment"    androID:ID="@+ID/detail"    androID:layout_weight="2"/>

2.在Activity运行时添加Fragment,主要修改的是Main中的代码

在Main中的layout中添加一个布局容器,把新建的Fragment丢到这个布局容器里就好

Main的layout

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="horizontal"    tools:context=".MainActivity">    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="Main"        >    </TextVIEw>    <FrameLayout        androID:ID="@+ID/frame"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        >    </FrameLayout></linearLayout>

Main的java文件

package com.example.myactivityiiii;import androIDx.appcompat.app.AppCompatActivity;import androIDx.fragment.app.Fragment;import androIDx.fragment.app.FragmentTransaction;import androID.os.Bundle;/** Activity运行时添加Fragment* */public class MainActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        DetailFragment detailFragment = new DetailFragment();        //获取Fragment的transaction实例        FragmentTransaction ft= getSupportFragmentManager().beginTransaction();        //指定一个容器来添加detailFragment        ft.add(R.ID.frame,detailFragment);        //提交事物        ft.commit();    }}

 

总结

以上是内存溢出为你收集整理的Android基础——Fragment的创建和添加全部内容,希望文章能够帮你解决Android基础——Fragment的创建和添加所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存