Androi开发 ---Fragment片段布局案例
效果图:
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片段布局案例的更多相关文章
- Android开发 ---Fragment片段布局
前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...
- Android开发---网格布局案例
Android开发---网格布局案例 效果图: 1.MainActivity.java package com.example.android_activity; import android.ap ...
- Android开发——Fragment知识整理(一)
0. 前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- 慕课网5-2编程练习:flex布局制作卡片布局案例
慕课网5-2编程练习:flex布局制作卡片布局案例 小伙伴们,学习了卡片布局,接下来我们根据效果图,也写出一个卡片布局的页面吧! 效果图如下: 任务 1.主体内容的卡片一行只能显示两个. 2.卡片与卡 ...
- Android(java)学习笔记164:Relativelayout相对布局案例
我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...
- 移动端页面开发适配 rem布局原理
主题 HTML移动端页面开发适配 rem布局原理 什么是适配,为什么要适配 我们拿到的设计图一般是以640,750,1080分辨率为基准设计的,而现在的手机终端各式各样,分辨率不同,逻辑像素不同 ,适 ...
- Android开发——Fragment知识整理(二)
0. 前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...
- 安卓app开发-05-Android xml布局详细介绍
安卓app开发-05-Android xml布局详细介绍 虽然说有 墨刀,墨客 这些图形化开发工具来做 Android 的界面设计,但是我们还是离不开要去学习做安卓原生app,学习 xml 布局还是必 ...
随机推荐
- change_bit 按位取反
int change_bit(int nr, void * addr){ int oldbit; //1.第nr位取反, 原nr位入CF //2. sbbl带借位减(把源操作数和标志 ...
- Can't find kernel text map area from kcore
kernel : 4.9.51kexec-tools : 2.0.4 1.~ # ./kexec -p /boot/vmlinuz --initrd=/boot/initrd --command-li ...
- Android Studio 一直卡在building解决办法
1.随便找一个你能运行的as项目 2.打开gradle-wrapper.properties,文件目录:项目/gradle/wrapper/gradle-wrapper.properties 3.复制 ...
- android--------自定义控件 之 组合控件篇
上篇介绍了自定义控件的自定义属性篇,地址:http://www.cnblogs.com/zhangqie/p/8969163.html 这篇博文主要来说说 自定义控件的组合控件来提高布局的复用 使用自 ...
- php实现队列
<?php //双向队列的实现 class DoubleEndedQueue{ public $elements; public function __construct(){//析构函数,创建 ...
- xgboost原理及应用--转
1.背景 关于xgboost的原理网络上的资源很少,大多数还停留在应用层面,本文通过学习陈天奇博士的PPT地址和xgboost导读和实战 地址,希望对xgboost原理进行深入理解. 2.xgboos ...
- linux导出sql数据
1. 导出数据库的数据 在linux命令行下输入 mysqldump -u userName -p dabaseName > fileName.sql 在linux命令行下输入 2. 导出表 ...
- 谈一谈HashMap类2
1.由一个小案例引出本博文的讨论 public class Demo1 { public static void main(String[] args) throws Exception { Stud ...
- spring boot 2.0(二)动态banner的支持
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...
- python-django rest framework框架之dispatch方法源码分析
1.Django的 CBV 中在请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法 class APIView(View): def ...