Fragment的使用

Fragment的使用分为静态使用和动态使用。使用Fragment在导包时,有App包和V4包两种情况。这两种情况的显示效果都是一样的,只不过因为Fragment是在Android 3.0以上引入的,V4包下的Fragment可以兼容3.0以下的版本,而App包下的则不可以。值得注意的是,如果使用APP包下的Fragment,那么Activity是可以直接使用Activity的;而如果使用V4包下的Fragment,那么对应的Activity是不可以使用Activity的,必须使用FragmentActivity或者它的子类。现在分别使用下这两种Fragment:

一、静态使用(以V4包下的Fragment为例):

1.静态加载Fragment的步骤:

(1)定义Fragment的布局,就是Fragment显示内容的;

(2)自定义一个Fragment类,需要继承Fragment或者它的子类,重写onCreateView()方法;

(3)在需要加载Fragment的Activity对应的布局文件中添加Fragment标签。

2.代码如下:

(1)定义一个Fragment布局fragment_up:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="#850099">
    <TextView
        android:text="这是Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


(2).自定义一个Fragment类UpFragment:


package com.example.administrator.fragment_static;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/3/21.
 */
public class UpFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView=inflater.inflate(R.layout.fragment_up,container,false);
        return contentView;
    }
}


 (3)在Activity中加载Fragment    activity_main: 

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.fragment_static.MainActivity"
    android:orientation="vertical"
    android:weightSum="2">

    <fragment
        android:name="com.example.administrator.fragment_static.UpFragment"
        android:id="@+id/fragment_up"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
</LinearLayout>


MainActivity:


package com.example.administrator.fragment_static;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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


二、动态使用(以App包下的为例):

1.动态加载Fragment的步骤:

(1)获得FragmentManager对象;(所有Fragment的管理方法都在FragmentManager中)

(2)获得FragmentTranscation对象;(对Fragment进行添加,移除,替换,以及执行其他动作)

(3)调用add方法或者replace方法加载Fragment对象;

(4)调用commit方法来提交事务。

2.代码如下:

(1)布局文件activity_main:fragment在动态加载时需要一个占位布局,而通常选择FragmentLayout作为Fragment的占位布局,因为它是最简单的一种布局,所有的内容都依次往左上角添加:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.fragment_static.MainActivity"
    android:orientation="vertical"
   >


    <Button
        android:id="@+id/btn_a"
        android:text="添加FragmentA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_b"
        android:text="添加FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:layout_weight="1"
        android:id="@+id/fragment_content"
        android:layout_width="wrap_content"
        android:layout_height="0dp"></FrameLayout>
</LinearLayout>


(2)新建AFragment类和BFragment类分别继承Fragment,其对应的布局分别为fragment_a和fragment_b:


fragment_a:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="这是A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


AFragment:

package com.example.administrator.fragment_dy;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/3/21.
 */
public class AFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a,container,false);
    }
}


B类似,把A改为B即可。


(3)在MainActivity中动态加载:

package com.example.administrator.fragment_dy;

import android.app.Activity;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{
    private Button aBtn,bBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.aBtn=(Button)super.findViewById(R.id.btn_a);
        this.bBtn=(Button)super.findViewById(R.id.btn_b);
        aBtn.setOnClickListener(this);
        bBtn.setOnClickListener(this);
    }
    public void onClick(View view){
        android.app.FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        switch (view.getId()){
            case R.id.btn_a:
                fragmentTransaction.replace(R.id.fragment_content,new AFragment());
                break;
            case R.id.btn_b:
                fragmentTransaction.replace(R.id.fragment_content,new BFragment());
                break;
        }
        fragmentTransaction.commit();
    }
}










内容概要:本文详细介绍了基于FPGA的144输出通道可切换电压源系统的设计与实现,涵盖系统总体架构、FPGA硬件设计、上位机软件设计以及系统集成方案。系统由上位机控制软件(PC端)、FPGA控制核心和高压输出模块(144通道)三部分组成。FPGA硬件设计部分详细描述了Verilog代码实现,包括PWM生成模块、UART通信模块和温度监控模块。硬件设计说明中提及了FPGA选型、PWM生成方式、通信接口、高压输出模块和保护电路的设计要点。上位机软件采用Python编写,实现了设备连接、命令发送、序列控制等功能,并提供了一个图形用户界面(GUI)用于方便的操作和配置。 适合人群:具备一定硬件设计和编程基础的电子工程师、FPGA开发者及科研人员。 使用场景及目标:①适用于需要精确控制多通道电压输出的实验环境或工业应用场景;②帮助用户理解和掌握FPGA在复杂控制系统中的应用,包括PWM控制、UART通信及多通道信号处理;③为研究人员提供一个可扩展的平台,用于测试和验证不同的电压源控制算法和策略。 阅读建议:由于涉及硬件和软件两方面的内容,建议读者先熟悉FPGA基础知识和Verilog语言,同时具备一定的Python编程经验。在阅读过程中,应结合硬件电路图和代码注释,逐步理解系统的各个组成部分及其相互关系。此外,实际动手搭建和调试该系统将有助于加深对整个设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值