效果图:

1、MainActivity.java

package com.example.android_activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//片段布局
public void test_3(View view){
Intent intent=new Intent(this,WeixinActivity.class);
startActivity(intent);
}
}

2、WeixinActivity.java

package com.example.android_activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup; public class WeixinActivity extends Activity { //片段管理器
private FragmentManager fm;
//片段事务对象
private FragmentTransaction trans; //获取单选按钮组
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_weixin);
rg=(RadioGroup)findViewById(R.id.radgroup); //1:获取片段管理器对象
fm=getFragmentManager();
//2:获取片段事务对象
trans=fm.beginTransaction();
trans.add(R.id.mainFrame,new WeixinFragment());
trans.commit(); //添加选择改变事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.rad_1:
trans=fm.beginTransaction();
trans.replace(R.id.mainFrame,new WeixinFragment());
trans.addToBackStack("back");
trans.commit();
break;
case R.id.rad_2:
trans=fm.beginTransaction();
trans.replace(R.id.mainFrame,new PhoneNoteFragment());
trans.addToBackStack("back");
trans.commit();
break;
case R.id.rad_3:
trans=fm.beginTransaction();
trans.replace(R.id.mainFrame,new FindFragment());
trans.addToBackStack("back");
trans.commit();
break;
}
}
});
} }

3、WeixinFragment.java

package com.example.android_activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; public class WeixinFragment extends Fragment { public WeixinFragment() {
} private Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//投影【将一个Layout布局xml文件生成一个View对象】
View view=inflater.inflate(R.layout.fragment_weixin, container, false);
btn=(Button)view.findViewById(R.id.test_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),"OK",Toast.LENGTH_LONG).show();
}
});
return view;
} }

4、PhoneNoteFragment.java

package com.example.android_activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* A simple {@link Fragment} subclass.
*/
public class PhoneNoteFragment extends Fragment { public PhoneNoteFragment() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_phone_note, container, false);
} }

5、FindFragment.java

package com.example.android_activity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* A simple {@link Fragment} subclass.
*/
public class FindFragment extends Fragment { public FindFragment() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find, container, false);
} }

6、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FrameLayout"
android:onClick="test_1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FrameLayout2"
android:onClick="test_2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment案例"
android:onClick="test_3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GridLayout布局[4.0]"
android:onClick="test_4"
/>
</LinearLayout>

7、activity_weixin.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_weixin"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--绘制内容显示容器-->
<FrameLayout
android:id="@+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"/> <LinearLayout
android:id="@+id/weixinMenu"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/radgroup"
android:checkedButton="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/rad_1"
android:text="微信"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rad_2"
android:text="通讯录"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rad_3"
android:text="发现"
android:gravity="center"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rad_4"
android:text="我"
android:gravity="center"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_above="@id/weixinMenu"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"></LinearLayout>
</RelativeLayout>

8、fragment_weixin.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="我是微信" />
<Button
android:id="@+id/test_btn"
android:text="点我试试"
android:onClick="testClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

9、fragment_phone_note.xml

<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"
> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:textColor="@android:color/holo_red_dark"
android:text="这是通讯录" /> </LinearLayout>

10、fragment_find.xml

<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="com.example.FindFragment"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textColor="@android:color/holo_green_light"
android:text="我发现好多朋友" /> </FrameLayout>

Androi开发 ---Fragment片段布局案例的更多相关文章

  1. Android开发 ---Fragment片段布局

    前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...

  2. Android开发---网格布局案例

     Android开发---网格布局案例 效果图: 1.MainActivity.java package com.example.android_activity; import android.ap ...

  3. Android开发——Fragment知识整理(一)

    0.  前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...

  4. Android开发——Fragment的简单使用总结

    前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...

  5. 慕课网5-2编程练习:flex布局制作卡片布局案例

    慕课网5-2编程练习:flex布局制作卡片布局案例 小伙伴们,学习了卡片布局,接下来我们根据效果图,也写出一个卡片布局的页面吧! 效果图如下: 任务 1.主体内容的卡片一行只能显示两个. 2.卡片与卡 ...

  6. Android(java)学习笔记164:Relativelayout相对布局案例

    我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...

  7. 移动端页面开发适配 rem布局原理

    主题 HTML移动端页面开发适配 rem布局原理 什么是适配,为什么要适配 我们拿到的设计图一般是以640,750,1080分辨率为基准设计的,而现在的手机终端各式各样,分辨率不同,逻辑像素不同 ,适 ...

  8. Android开发——Fragment知识整理(二)

    0.  前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...

  9. 安卓app开发-05-Android xml布局详细介绍

    安卓app开发-05-Android xml布局详细介绍 虽然说有 墨刀,墨客 这些图形化开发工具来做 Android 的界面设计,但是我们还是离不开要去学习做安卓原生app,学习 xml 布局还是必 ...

随机推荐

  1. 雷林鹏分享:现实生活中的 XML

    现实生活中的 XML 如何使用 XML 来交换信息的一些实例. 实例:XML 新闻 XMLNews 是用于交换新闻和其他信息的规范. 对新闻的供求双方来说,通过使用这种标准,可以使各种类型的新闻信息通 ...

  2. 20165327《Java程序设计》实验一 Java开发环境的熟悉 实验报告

    20165327<Java程序设计>实验二 <Java面向对象程序设计>实验报告 实验二 <Java面向对象程序设计> 一.实验报告封面 课程:Java程序设计 班 ...

  3. navicat和 plsql 连接oracle数据库 总结

    打开 navicat  -->工具-->选项-->oci   右侧选择oci.dll 的路径 默认 在 navicat的安装目录下有一个 instantclient 的文件夹 直接选 ...

  4. hdu-2089 不要62 基础DP 模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位DP的思想时预处理以x开头长度为len的数(例如 x000~x999)的所有情况.给出一个数,可以在l ...

  5. Beautiful Paintings CodeForces - 651B (贪心)

    大意: 给定序列$a$, 可以任意排序, 求最大下标i的个数, 满足$a_i<a_{i+1}$. 这个贪心挺好的, 答案就为n-所有数字出现次数最大值.

  6. Tanya and Password CodeForces - 508D (欧拉回路)

    大意:给定n个长为3的子串, 求一个长为n+2的字符串包含所有子串. 相邻两个字符开一个节点, 建图跑欧拉回路, 若存在的话长度一定是$\le n+2$.

  7. 20165309 实验四 Android程序设计

    2017-2018-2 20165309实验四<Java面向对象程序设计>实验报告 一.实验内容 1.Android Studio的安装测试 2.Activity测试 3.UI测试 4.布 ...

  8. sublime 3的破解和安装

    http://www.xue51.com/mac/1518.html 啥都别问,问就是按照上面的网址操作就行,本人亲测可用.

  9. win10更新后,可以远程桌面ping也没问题,但是无法访问共享文件夹的解决方法

    计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters 1.在键盘输入WIN+R键, ...

  10. 谈一谈HashMap类

    一.Java中的hashCode()和equals() 1. hashCode()的存在主要是用于查找的快捷性,如Hashtable,HashMap等,hashCode()是用来在散列存储结构中确定对 ...