Android Fragment

在 Activity 中添加 Fragment

向 Activity 中添加 Fragment,有两种方法:一种是直接在布局文件中添加,将 Fragment 作为
Activity 整个布局的一部分;另一种是当 Activity 运行时,将 Fragment 放入 Activity 布局中。

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal" >
<fragment android:name="com.ListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.DetailFragment"
android:id="@+id/detail"
android:layout_weight="2"
android:layout_marginLeft="20dp"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

代码

DetailFragment details = new DetailFragment(); //实例化DetailFragment的对象
FragmentTransaction ft = getFragmentManager()
.beginTransaction(); //获得一个FragmentTransaction的实例
ft.add(android.R.id.content, details); //添加一个显示详细内容的Fragment
ft.commit(); //提交事务

示例:

入口类

 
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView i1 = findViewById(R.id.a1);
        ImageView i2 = findViewById(R.id.a2);
        ImageView i3 = findViewById(R.id.a3);
        i1.setOnClickListener(this);
        i2.setOnClickListener(this);
        i3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //获取fragment
         FragmentManager fm=getSupportFragmentManager();
        //开启一个事务
         FragmentTransaction ft=fm.beginTransaction();
        //初始化fragment
        Fragment f=null;

        switch (v.getId()){
            case R.id.a1:
                f=new WeChatFragment() ;
                Toast.makeText(getApplicationContext(), "a1", Toast.LENGTH_LONG).show();
                break;
            case R.id.a2:
                f=new MeFragment();
                break;
            case R.id.a3:
                f=new MsgFragment();
                break;
            default:
                break;
        }

        ft.replace(R.id.fragment, f);  //替换fragment
        ft.commit(); //提交事务


    }
}

主布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity"
    tools:ignore="MissingConstraints"
    android:background="@color/white">

    <fragment
        android:id="@+id/fragment"
        android:name="com.WeChatFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
       >

        <ImageView
            android:id="@+id/a1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/a1"/>

        <ImageView
            android:id="@+id/a2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/a2"/>

        <ImageView
            android:id="@+id/a3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/a3"/>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment碎片


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me, container, false);
    }
}

碎片布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MeFragment"
    android:background="@color/white">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/me_fragment" />

</FrameLayout>

注意项

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
         //获取fragment
         FragmentManager fm=getSupportFragmentManager();
        //开启一个事务
         FragmentTransaction ft=fm.beginTransaction();
 android:background="@color/white"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人海中的海盗

你的打赏将是对我的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值