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产生原因,以及一些基本的用法和 ...
随机推荐
- FlashBack-SCN-TIMESTAMP
一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...
- VisualStudio.DTE 对象可以通过检索 GetService() 方法
DTE dte = (DTE)GetService(typeof(DTE)); string solutionDir = System.IO.Path.GetDirectoryName(dte.Sol ...
- 远程调试weinre的使用
一.用途 *鉴于在浏览器调试移动端页面无法准确反映移动端实际情况并无法高效调试,故常常使用远程调试工具通过电脑连接手机进行调试,常用远程调试方式: 1.chrome连接安卓机远程调试 2.Mac连接苹 ...
- for循环删除集合陷阱
首先看下面的代码: import java.util.LinkedList;import java.util.List; public class DeleteCollection { ...
- 查看DB文件的空间使用情况
可以使用如下语句获得DB文件的空间使用 use dbName SELECT DB_NAME() AS DbName, name AS FileName, size/128.0 AS CurrentSi ...
- java static关键字
方便在没有创建对象的情况下来进行调用(方法/变量). 很显然,被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static可以用来修饰类的 ...
- android ellipsize 属性详解
TextView中内容过长时添加省略号的属性,即ellipsize 用法如下: 在XML文件中设置: android:ellipsize = "end" //省略号在结尾 andr ...
- 修改Windows系统的启动Shell
前提:当前系统中有可用的shell文件 方法: 修改当前用户的系统默认shell(只对当前用户生效,且优先于本机默认的shell) 修改“HKCU\SOFTWARE\Microsoft\Windo ...
- 模块化开发之sea.js实现原理总结
seajs官网说:seajs是一个模块加载器,所以学习它并不难. 在我的理解就是:本来我们是需要手动创建 script标签 引入 js文件的,但用seajs后,它就自动帮我们完成这些工作. 这里只说实 ...
- vs2005的MFC程序在64位机上不能运行
出现上述错误大多因为该机上没有安装vs2005程序,vs2005要运行需要一些必要的文件,没有添加到你发布的可执行文件目录下,所以程序不能运行. 解决方法: 安装目录\Microsoft Visual ...

