Android 基础:Fragment的介绍与应用,QQ底栏,侧滑菜单

在Activity的布局文件中,将Fragment作为一个子标签加入即可。

<?xml version="1.0" encoding="utf-8"?>

加载方式2:通过编程的方式将Fragment加入到一个ViewGroup中

当Activity处于Running状态下的时候,可以在Activity的布局中动态地加入Fragment,只需要指定加入这个Fragment的父View Group即可。

首先,需要一个FragmentTransaction实例:

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

(注,如果importandroid.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

之后,用add()方法加上Fragment的对象:

ExampleFragment fragment = new ExampleFragment();

fragmentTransaction.add(R.id.fragment_container, fragment);

fragmentTransaction.commit();

其中第一个参数是这个fragment的容器,即父控件组。

最后需要调用commit()方法使得FragmentTransaction实例的改变生效。

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”

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。

【Android思维脑图(技能树)】

知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。

【Android高级架构视频学习资源】

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。

【Android思维脑图(技能树)】

知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。

[外链图片转存中…(img-7oxnbBiZ-1712517617462)]

【Android高级架构视频学习资源】

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 25
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值