02Android用户界面优化之(一)Android Fragment
一、使用Fragment
1.AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.learningfragment"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
2.布局文件
(1)activity_main.xml
其中放置的是一个帧布局的布局容器,其id为container
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/container">
</FrameLayout>
(2)fragment_main.xml
主fargment布局文件,里面放置的按钮用于弹出另外一个fragment
<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"
android:orientation="vertical"
tools:context="com.example.shiyanshi.learningfragment.MainActivityFragment"
tools:showIn="@layout/activity_main"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京欢迎您!" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnShowFragmentAnother"
android:text="显现另外一个Fragment"/> </LinearLayout>
(3)fragment_another.xml
从fragment布局文件,里面的按钮用于支持返回到上一个fragment界面
<?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:id="@+id/fragmentAnother"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是另外一个Fragment"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnBackOperation"
android:text="后退操作"/> </LinearLayout>
3.Java源文件
(1)MainActivity.java
package com.example.shiyanshi.learningfragment; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container,new MainActivityFragment()) //add的第一个参数是布局容器,第二个参数是主Fragment的类
.commit();
}
} }
(2)MainActivityFragment.java
package com.example.shiyanshi.learningfragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends
Fragment
{ //注意其继承的是
import android.support.v4.app.Fragment
中的Fragment
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_main, container, false); //第一个参数:主fragment的布局文件,第二个:布局容器,第三个为布尔型attachToRoot
rootView.findViewById(R.id.btnShowFragmentAnother).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.addToBackStack(null) //加入后退栈,使其支持后退的功能
.replace(R.id.container,new AnotherFragment())
.commit();
}
});
return rootView;
}
}
(3)AnotherFragment.java
package com.example.shiyanshi.learningfragment; 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 shiyanshi on 2016/1/25.
*/
public class AnotherFragment extends
Fragment
{
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_another,container,false);
view.findViewById(R.id.btnBackOperation).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
return view;
}
}
4.界面显示效果
二、Fragment的生命周期
02Android用户界面优化之(一)Android Fragment的更多相关文章
- Android编程权威指南笔记3:Android Fragment讲解与Android Studio中的依赖关系,如何添加依赖关系
Android Fragment 当我在学习时,了解了Fragment词汇 Fragment是一种控制器对象,我就把所了解的简单说一下.activity可以派fragment完成一些任务,就是管理用户 ...
- Android: Fragment编程指南
本文来自于www.lanttor.org Fragment代表了Activity里的一个行为,或者Activity UI的一部分.你可以在一个activity里构造多个Fragment,也可以在多个a ...
- 【Android自学日记】【转】Android Fragment 真正的完全解析(下)
上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API,如果你还不了解,请看:Android Fragment 真正的完全解析(上). 本篇将介绍上篇博客提到的:如何管理Frag ...
- Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...
- Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment使用(一) 基础篇 温故知新
Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment 真正的完全解析(下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...
随机推荐
- MFC学习之程序执行过程梳理
*首先利用全局变量对象theApp启动应用程序.这是由于这个全局对象,基类CWinApp中this的指针才干指向这个对象.假设没有这个全局对象,程序在编译时不会出错,但在执行时就会出错. *调用全局应 ...
- 深入浅出NodeJS——异步I/O
底层操作系统,异步通过信号量.消息等方式有着广泛的应用. PHP语言从头到尾都是以同步堵塞方式执行,利于程序猿顺序编写业务逻辑. 异步I/O.事件驱动.单线程构成Node的基调. why异步I/O ( ...
- LFS: Interface eth0 doesn't exist
环境 宿主主机:Ubuntu 14.04.4 LTS 32位 LFS内核:Linux 4.2.0 好不用容易将LFS引导起来了,但系统启动后,无法配置网口.系统启动时提示:Interface eth0 ...
- textarea高度自适应问题
textarea中的文字如果过多,就会产生滚动条,一本分文本被遮盖住,不能看到所有的文本. 那么,如何才能让textarea的高度随输入内容多少,可以自动的改变高度呢? 解决思想: 1 利用conte ...
- [原创] Assistant editor 取消拖拽 binding 的 UI 元素
1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...
- JSON之三:获取JSON文本并解释(以google的天气API为例)
google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: { ...
- Struts2注解学习1
这是开博的第一篇,我希望每天把我学到的东西记录下来,成为一个知识库,方便以后的学习和分享 在项目中看到用struts2注解来做,很方便,做了一个用户登录的例子 1.加载所需jar包 commons-f ...
- php composer使用
Composer-PHP中用来管理依赖(dependency) 定义 composer是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(librar ...
- Fiddler 域名过滤
原来一直没意识到Fiddler过滤,导致每次抓包都要自己判断.搜索好多东西,真是呵呵! 过滤设置很简单,看懂一张图就解决问题了. 箭头 那两处设置下,圆圈处保存再进行抓包即可
- 实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域
首先肯定要先看看这两部分是不是有内存重叠?为什么? 1.因为如果有内存重叠(目的地址起始位置处于源指针所指区域之中),你再从起始位置复制的话,这样目的地址改变的时候将源地址内存里面存的东西给改变了,所 ...

