Fragment的用法(类似于.net的用户控件,重用性好)。。。。---------------android

就想winform里的用户控件一样,你可以在一个页面初始化的时候,直接就在指定位置加一个用户控件,也可以更具数据来动态添加。

对于安卓里的fragment,也有这两种方式,一个叫静态加载,一个叫动态加载。

不管是静态还是动态,首先要有一个fragment的activity

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.myapplication.fragment">

<TextView android:text="你好" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/sk"/>

</RelativeLayout>
package com.example.administrator.myapplication;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class fragment extends Fragment { //必须继承这个类

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return super.onCreateView(inflater, container, savedInstanceState);
    }
}

然后再建一个activity来展示静态和动态的方法加载fragment

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.myapplication.Main2Activity"
    >
    <fragment
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.administrator.myapplication.fragment"
        android:tag="这里是静态加载,上面的那个参数name,是fragment的类">
    </fragment>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame"
        android:orientation="horizontal"
        android:tag="这里是动态加载,只需要有个id,后台根据这个id,在这个layout里面加载一个fragment">
    </LinearLayout>
</LinearLayout>

下面在这个页面的后台代码里,展示了静态加载和动态加载

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);//这个方法必须有,虽然填的不是当前页面,当时加载后的页面就是当前页面
        
        
        //以下是静态加载,在静态加载时,可以访问fragment里面的控件
        TextView textView=(TextView)findViewById(R.id.sk);
        textView.setText("你好");
        
        //一下是动态下载的详细步奏
        fragment fragment = new fragment();//实例化这个fragment的类
        FragmentManager fragmentManager = getFragmentManager() ;
        FragmentTransaction tran=fragmentManager.beginTransaction();//获取一个事务
        tran.add(fragment, null);//添加这个事务的内容
        tran.commit();//提交事务
    }
原文地址:https://www.cnblogs.com/xiaoleye/p/4770268.html