Fragment简单示例

这篇博客介绍了Android SDK 3.0引入的新特性Fragment,并提供了一个简单的示例。在示例中,当设备竖屏时显示橘黄色界面,横屏则变为灰色。博主还分享了使用Genymotion模拟器的体验,强调其相对于原生Android模拟器在开机速度、程序加载和运行流畅度上的优势。最后,博主预告将在下一篇文章中讲解如何安装和使用Genymotion。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fragment是SDK3.0以后才出来的新特性,网上关于它的例子很多,我学习的时候也查阅过很多种类型的,但是我发现没有哪个是我看完就自己会编的,现在我发上来我认为至少我一看就会编的程序上来供大家参考,当然程序肯定是我自己写的,首先先创建3个类,名字自己随便取,当然也要创建与之匹配3个布局文件,名字也随便,下面上代码:

FragmentDemo的Java代码:

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

public class FragmentDemo extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            //创建一个view对象,并获取布局文件
        View view = inflater.inflate(R.layout.fragmentdemo_layout,container,false);
        //返回的view就作为当前Activity的布局
        return view;
    }
}
然后与之对应的布局文件:

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

</LinearLayout>
FragmentDetail的Java代码:

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

public class FragmentDetail extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflater.inflate(R.layout.fragmentdemo_layout,null);
        View view =inflater.inflate(R.layout.fragmentdetail,container,false);
        return view;
    }
}

与之对应的布局文件:

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

</LinearLayout>

MainActivity的Java代码:

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.WindowManager;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化一个FragmentManager,并调用beginTransaction()方法
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //获取当前屏幕
        WindowManager manager = getWindowManager();
        Display display = manager.getDefaultDisplay();
           //获取并比较屏幕的宽和高
        if (display.getWidth()>display.getHeight()) {
            FragmentDemo fragmentDemo = new FragmentDemo();
            transaction.replace(android.R.id.content,fragmentDemo);
        }else {
            FragmentDetail fragmentDetail = new FragmentDetail();
            transaction.replace(android.R.id.content,fragmentDetail);
        }
        //调用commit()方法提交
            transaction.commit();
    }
}
与之对应的布局文件:

<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"
    tools:context=".MainActivity"
    android:orientation="horizontal">



</LinearLayout>

好了,代码部分完毕,大家会发现,这三个布局文件都只是采用LinearLayout方式布局,并没有写button或者textview之类的控件,当然写也可以,有兴趣的读者可以自己写写看,

该程序实现的功能就是如果竖屏,屏幕会显示橘黄色,横屏则会显示灰色;效果如图

:

到这里,其实关于Fragment的简单示例就差不多完了,但是为啥还要说呢,大家看到这个模拟器了没,我用的不是安卓自带的模拟器,安卓自带的模拟器开机速度和加载程序的速度我想用过的人都深有体会吧,那怎是一个慢字了得,所以果断抛弃了它,现在我用的是genymotion模拟器,我想用过的人也深有体会吧,我感觉genymotion和安卓原生模拟器就是一个在天上,一个在地下,无论是开机速度(genymotion开机大约也就10s--30s左右,原生模拟器至少90s以上吧),还是程序加载速度(genymotion从安装到显示出来也就5s左右,原生模拟器至少20s以上),还是运行速度(genymotion操作相当流畅,原生模拟器点一个button都一卡一卡的,并且还老容易"死机"),有人说genymotion用着这么爽,能不能装其他安卓应用或游戏呢,答案是肯定的,不仅可以装,并且玩的和真机一样爽,试想一下,开着多个模拟器,一边玩斗地主,一边玩五子棋,还一边登着qq聊天,那感觉....那么怎么装genymotion呢,请看我下一个博文-----超级安卓模拟器genymotion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值