Fragment简单的应用
=================
1、新建工程
2.、创建若干个简单的布局(用于fragment的加载),如下:
activiy_main为主布局文件,fragment1,fragment2为fragment的布局文件
3、创建 Fragment 的类
创建Fragment1:创建过程与创建Activity类似,不过继承的是android.support.v4.app.Fragment
复写onCreateView函数加载fragment的布局,R.layout.fragment1就是刚刚创建的布局文件。
创建 Fragment2 :同上
4、运行
Fragment扩展应用
================
底栏按钮切换界面的实现(类似QQ底栏)
1、与上面类似,新建布局文件activity2.xml,具体布局如下:
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical” >
<LinearLayout
android:id=“@+id/bottom”
android:layout_width=“match_parent”
android:layout_height=“65dp”
android:layout_alignParentBottom=“true”
android:background=“@null”
android:gravity=“center_vertical”
android:orientation=“horizontal” >
<Button
android:id=“@+id/f1”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:text=“fragment1” />
<Button
android:id=“@+id/f2”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:text=“fragment2” />
<FrameLayout <!-- 注意,这里是帧布局不是Fragment --!>
android:id=“@+id/content_fragment”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_above=“@+id/bottom”
android:background=“#fff” >
两个按钮组成的布局作为底栏,上面空白部分为帧布局:
2、新建两个用于填充Fragment的布局文件,与上面类似。
3、创建两个Fragment类,与上面类似
4、编写主Activity代码:
public class Activity2 extends FragmentActivity {//注意!这里继承的是FragmentActivity
private Button f1, f2;
private Fragment mContent; //这里是Fragment而不是帧布局,帧布局不用声明
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);//加载activity的布局
f1 = (Button) findViewById(R.id.f1);//实例化两个按钮
f2 = (Button) findViewById(R.id.f2);
if (mContent == null) {
mContent = new Fragment3(); //实例化Fragment
//下面是重点!
//Fragment通过FragmentManager来管理
//beginTransaction()返回Fragment事务管理实例
//replace()执行Fragment事务中的替换
//R.id.content_fragment用来装载Fragment的容器
//mContent用于替换的Fragment的对象
//commit()提交事务,完成操作
getSupportFragmentManager().beginTransaction().replace(
R.id.content_fragment, mContent).commit();
}
f1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mContent = new Fragment3();
if (mContent != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_fragment, mContent).commit();
}
}
});
f2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mContent = new Fragment4();
if (mContent != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_fragment, mContent).commit();
}
}
});
}
}
5、运行调试
1、侧滑菜单+Fragment
侧滑菜单的介绍
市场上很多应用均采取的框架,从屏幕左侧划出菜单,点击菜 单,右侧界面进行相应的切换。 安卓5.0以前实现这种效果只能通过别人的开源代码实现,从5.0安卓开始提供了自带的侧滑菜单组件DrawerLayout,该组件在supportv4包下,兼容之前的版本。
1、新建工程,与相应的布局文件
2、Activity的布局如下:
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical” >
<android.support.v4.widget.DrawerLayout
android:id=“@+id/drawer_layout”
android:layout_width=“match_parent”
android:layout_height=“match_parent” >
<FrameLayout
android:id=“@+id/content_frame”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
<LinearLayout
android:id=“@+id/left_drawer”
android:layout_width=“240dp”
android:layout_height=“match_parent”
android:layout_gravity=“start” 表示左边菜单
android:background=“#ffffff”
android:gravity=“center”
android:orientation=“vertical” >
<TextView
android:id=“@+id/one”
android:layout_width=“240dp”
android:layout_height=“55dp”
android:drawableLeft=“@drawable/ic_launcher”
android:gravity=“center”
android:text=“第一个界面”
android:textColor=“#353535”
android:textSize=“23dp” />
<TextView
android:id=“@+id/two”
android:layout_width=“240dp”
android:layout_height=“55dp”
android:drawableLeft=“@drawable/ic_launcher”
android:gravity=“center”
android:text=“第二个界面”
android:textColor=“#353535”
android:textSize=“23dp” />
<TextView
android:id=“@+id/three”
android:layout_width=“240dp”
android:layout_height=“55dp”
android:drawableLeft=“@drawable/ic_launcher”
android:gravity=“center”
android:text=“第三个界面”
android:textColor=“#353535”
android:textSize=“23dp” />
<TextView
android:id=“@+id/right_drawer”
android:layout_width=“280dp”
android:layout_height=“match_parent”
android:layout_gravity=“end” 表示右边菜单
android:background=“#ffffff”
android:gravity=“center”
android:text=“@string/right_drawer_info”
android:textColor=“#444444”
android:textSize=“30sp” >
</android.support.v4.widget.DrawerLayout>
3、主Acitity的编写
最后
分享一份工作1到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!
-
阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升
-
**全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记
有任何问题,欢迎广大网友一起来交流
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
到5年以上的Android程序员架构进阶学习路线体系,希望能对那些还在从事Android开发却还不知道如何去提升自己的,还处于迷茫的朋友!**
-
阿里P7级Android架构师技术脑图;查漏补缺,体系化深入学习提升
[外链图片转存中…(img-bXufADJu-1714309084225)]
-
**全套体系化高级架构视频;**七大主流技术模块,视频+源码+笔记
[外链图片转存中…(img-uvnltdTQ-1714309084226)]
有任何问题,欢迎广大网友一起来交流
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!