在做项目中,需要建立一个主界面框架,尝试过使用ViewPager ,后来又换成了使用Activity动态加载Fragment实现选项卡的效果。总结一下方便以后回顾。

先给出总体效果:

要实现上述效果,首先来大体上阐述步骤:

步骤一:

创建一个界面框架布局文件activity_frame.xml ,一个垂直现行布局包含:从上到下第一个帧布局FrameLayout用于动态加载Fragment,命其id为content;第二个布局是一个相对布局,用于放置三个标签按钮,其中三个线性布局嵌套分别一个ImageView,为了能均分,权重都为1;图标自己制作。同时创建一个FrameActivity.java文件,来显示主界面框架。

activity_frame.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#c0c0c0" > <RelativeLayout
android:id="@+id/main_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" > <ImageView
android:id="@+id/main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/img_main" />
</LinearLayout>
</RelativeLayout> <RelativeLayout
android:id="@+id/shop_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" > <ImageView
android:id="@+id/shop_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/img_shop" />
</LinearLayout>
</RelativeLayout> <RelativeLayout
android:id="@+id/my_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" > <ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/img_my" />
</LinearLayout>
</RelativeLayout>
</LinearLayout> </LinearLayout>

步骤二:

分别创建三个布局文件:

其中布局内容根据自己想要的布局设置;

步骤三:
    然后再为这三个布局创建对应的Fragment:

其中每个Fragment加载布局的代码都是差不多,都是在onCreateView中获得一个View对象,其它代码根据业务需要而编写,如图:

步骤四:

三个Fragment创建好了之后,此时需要在FrameActivity中,通过界面底边的三个标签注册单击事件监听器来动态地加载Fragment,

业务代码如下:

        @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.main_layout:
setTabSelection(0);
break;
case R.id.shop_layout:
setTabSelection(1);
break;
case R.id.my_layout:
setTabSelection(2);
break;
default:
break;
} <pre name="code" class="java">private void setTabSelection(int i) {
// TODO Auto-generated method stub
clearSelection();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (i) {
case 0:
mainImg.setImageResource(R.drawable.img_main_pressed);
if (mainFragment == null) {
mainFragment = new MainFragment();
transaction.add(R.id.content, mainFragment);
} else {
transaction.show(mainFragment);
}
break;
case 1:
shopImg.setImageResource(R.drawable.img_shop_pressed);
if (shopFragment == null) {
shopFragment = new ShopFragment();
transaction.add(R.id.content, shopFragment);
} else {
transaction.show(shopFragment);
}
break;
case 2:
default:
myImg.setImageResource(R.drawable.img_my_pressed);
if (myFragment == null) {
myFragment = new MyFragment();
transaction.add(R.id.content, myFragment);
} else {
transaction.show(myFragment);
}
break;
}
transaction.commit(); }

<pre name="code" class="java"><span style="font-size:18px;"> setTabSelection中包含一个switch函数,根据判断id来动态加载fragment(动态加载fragent的步骤这里不给出,csdn有相应的文章):
FrameActivity的完整代码如下:</span> <pre name="code" class="java">package com.android.activity; import com.android.client.R;
import com.android.fragment.MainFragment;
import com.android.fragment.MyFragment;
import com.android.fragment.ShopFragment; import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView; public class FrameActivity extends Activity implements OnClickListener {
private MainFragment mainFragment;
private ShopFragment shopFragment;
private MyFragment myFragment;
private View mainLayout;
private View shopLayout;
private View myLayoutView;
private ImageView mainImg;
private ImageView shopImg;
private ImageView myImg;
/*
* private TextView mainTxt; private TextView shopTxt; private TextView
* myTxt;
*/ private FragmentManager fragmentManager; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_frame);
initViews();
fragmentManager = getFragmentManager();
setTabSelection(0); } private void setTabSelection(int i) {
// TODO Auto-generated method stub
clearSelection();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (i) {
case 0:
mainImg.setImageResource(R.drawable.img_main_pressed);
if (mainFragment == null) {
mainFragment = new MainFragment();
transaction.add(R.id.content, mainFragment);
} else {
transaction.show(mainFragment);
}
break;
case 1:
shopImg.setImageResource(R.drawable.img_shop_pressed);
if (shopFragment == null) {
shopFragment = new ShopFragment();
transaction.add(R.id.content, shopFragment);
} else {
transaction.show(shopFragment);
}
break;
case 2:
default:
myImg.setImageResource(R.drawable.img_my_pressed);
if (myFragment == null) {
myFragment = new MyFragment();
transaction.add(R.id.content, myFragment);
} else {
transaction.show(myFragment);
}
break;
}
transaction.commit(); } private void hideFragments(FragmentTransaction transaction) {
// TODO Auto-generated method stub
if (mainFragment != null) {
transaction.hide(mainFragment);
}
if (shopFragment != null) {
transaction.hide(shopFragment);
}
if (myFragment != null) {
transaction.hide(myFragment);
}
} private void clearSelection() {
// TODO Auto-generated method stub
mainImg.setImageResource(R.drawable.img_main);
// mainTxt.setTextColor(Color.parseColor("#82858b"));
shopImg.setImageResource(R.drawable.img_shop);
// shopTxt.setTextColor(Color.parseColor("#82858b"));
myImg.setImageResource(R.drawable.img_my);
// myTxt.setTextColor(Color.parseColor("#82858b")); } private void initViews() {
// TODO Auto-generated method stub
mainLayout = findViewById(R.id.main_layout);
shopLayout = findViewById(R.id.shop_layout);
myLayoutView = findViewById(R.id.my_layout);
mainImg = (ImageView) findViewById(R.id.main_image);
shopImg = (ImageView) findViewById(R.id.shop_image);
myImg = (ImageView) findViewById(R.id.my_image);
/*
* mainTxt = (TextView) findViewById(R.id.main_text); shopTxt =
* (TextView) findViewById(R.id.shop_text); myTxt = (TextView)
* findViewById(R.id.my_text);
*/ mainLayout.setOnClickListener(this);
shopLayout.setOnClickListener(this);
myLayoutView.setOnClickListener(this); } @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.main_layout:
setTabSelection(0);
break;
case R.id.shop_layout:
setTabSelection(1);
break;
case R.id.my_layout:
setTabSelection(2);
break;
default:
break;
}
// TODO Auto-generated method stub }
//框架中的退出提示代码,常规代码重用率很高可以抽象出来
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Builder builder = new Builder(FrameActivity.this);
builder.setTitle("提示");
builder.setMessage("你确定要退出吗?");
builder.setIcon(R.drawable.ic_launcher); DialogInterface.OnClickListener dialog = new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
if (arg1 == DialogInterface.BUTTON_POSITIVE) {
arg0.cancel();
} else if (arg1 == DialogInterface.BUTTON_NEGATIVE) {
FrameActivity.this.finish();
}
}
};
builder.setPositiveButton("取消", dialog);
builder.setNegativeButton("确定", dialog);
AlertDialog alertDialog = builder.create();
alertDialog.show(); }
}
return false;
} }


到此,界面框架已经建好,接下来就根据自己的业务需要些代码了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

通过Activity动态加载Fragment创建主界面构架的更多相关文章

  1. 在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。

    在Fragment的生命周期中,需要重点关注onCreate.onCreateView.onViewCreated.Activity与Fragment生命周期在设计模式上大体一致. package c ...

  2. 关于Android中Fragment静态和动态加载的方法

    一.静态加载 1.首先创建一个layout布局fragment.xml,里面放要显示和操作的控件 2.创建一个layout布局main1.xml,用来实现页面的跳转(跳转为要实现静态加载的界面) 3. ...

  3. Android中的动态加载机制

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  4. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  5. Android之Android apk动态加载机制的研究(二):资源加载和activity生命周期管理

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 为了 ...

  6. Android学习——Fragment动态加载

    动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...

  7. Android中插件开发篇之----动态加载Activity(免安装运行程序)

    一.前言 又到周末了,时间过的很快,今天我们来看一下Android中插件开发篇的最后一篇文章的内容:动态加载Activity(免安装运行程序),在上一篇文章中说道了,如何动态加载资源(应用换肤原理解析 ...

  8. 动态加载Layout 与 论Activity、 Window、View的关系

    1)动态加载Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  9. 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。

    (转载声明出处:http://www.cnblogs.com/linguanh/) 简介:      该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...

随机推荐

  1. 初码-Azure系列-存储队列的使用与一个Azure小工具(蓝天助手)

    初码Azure系列文章目录 将消息队列技术模型简化,并打造成更适合互联网+与敏捷开发的云服务模式,好像已经是行业趋势,阿里云也在推荐使用消息服务(HTTP协议为主)而来替代消息队列(TCP协议.MQT ...

  2. c#实现自然排序效果,按1,2,11而不是1,11,12,区分字母文字和数字

    排序有时候要考虑后缀.这样看起来比较自然. 参考了codeproject上一篇文章:http://www.codeproject.com/Articles/22978/Implementing-the ...

  3. Yii 2.0.3 Advanced版控制器不能包含大写字母的Bug

    Yii 2.0.3 Advanced版控制器不能包含大写字母的Bug,我是直接下载Archive文件安装的,非Composer方式安装 Yii 框架之前是支持在Url中包含大写字母的 最新的Yii 2 ...

  4. ABP官方文档翻译 4.3 校验数据传输对象

    校验数据传输对象 校验简介 使用数据标注 自定义校验 禁用校验 标准化 校验简介 应用的输入首先应该被校验.输入可以是用户的也可以是其他应用的.在一个web应用中,校验通常实现两次:客户端和服务端.客 ...

  5. ABP官方文档翻译 2.6 定时

    定时 介绍 时钟 客户端 时区 客户端 Binders和Converters 介绍 一些应用只针对一个时区,而其他的一些已用则有许多不同的时区.为了满足这样的需求和集中的时间操作,Abp提供了时间操作 ...

  6. 转载-Oracle ORACLE的sign函数和DECODE函数

    原文地址:http://www.cnblogs.com/BetterWF/archive/2012/06/12/2545829.html 转载以备用 比较大小函数 sign 函数语法:sign(n) ...

  7. java web 获取客户端操作系统信息

    package com.java.basic.pattern; import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

  8. 在eclipse的配置文件里指定jdk路径

    在eclipse的配置文件里指定jdk路径,只需在eclipse的配置文件里增加-vm参数即可. 打开eclipse目录下的eclipse.ini配置文件,增加-vm配置,需要注意的是该参数要加在-v ...

  9. CF487 E. Tourists [点双连通分量 树链剖分 割点]

    E. Tourists 题意: 无向连通图 C a w: 表示 a 城市的纪念品售价变成 w. A a b: 表示有一个游客要从 a 城市到 b 城市,你要回答在所有他的旅行路径中最低售价的最低可能值 ...

  10. 在.NetCore中使用Myrmec检测文件真实格式

    Myrmec 是什么? Myrmec 是一个用于检测文件格式的库,Myrmec不同于其它库或者手写检测代码,Myrmec不依赖文件扩展名(在实际使用中,你的用户很可能使用虚假的扩展名欺骗你的应用程序) ...