【移动应用开发技术】怎么在Android 中利用Fragment实现底部菜单_第1页
【移动应用开发技术】怎么在Android 中利用Fragment实现底部菜单_第2页
【移动应用开发技术】怎么在Android 中利用Fragment实现底部菜单_第3页
【移动应用开发技术】怎么在Android 中利用Fragment实现底部菜单_第4页
【移动应用开发技术】怎么在Android 中利用Fragment实现底部菜单_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么在Android中利用Fragment实现底部菜单

本篇文章为大家展示了怎么在Android中利用Fragment实现底部菜单,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。第一步:添加引用引用Crosslight.Xamarin.Android.Support.v7.AppCompat这个包。第二步:绘制Main和Fragment界面fg_home.axml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF">

<TextView

android:id="@+id/txt_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="首页"

android:textColor="#000000"

android:textSize="20sp"

/>

</LinearLayout>fg_label.axml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF">

<TextView

android:id="@+id/txt_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="贴签"

android:textColor="#000000"

android:textSize="20sp"

/>

</LinearLayout>fg_mine.axml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF">

<TextView

android:id="@+id/txt_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="我的"

android:textColor="#000000"

android:textSize="20sp"

/>

</LinearLayout>fg_query.axml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF">

<TextView

android:id="@+id/txt_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="查询"

android:textColor="#000000"

android:textSize="20sp"

/>

</LinearLayout>Main.axml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<include

layout="@layout/main_left"

/>

</LinearLayout>main_left.xml<?xml

version="1.0"

encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout

xmlns:android="/apk/res/android"

android:id="@+id/dl_left"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#f0f0f0">

<!--主布局-->

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/relativelayout1"

android:fitsSystemWindows="true">

<RelativeLayout

android:id="@+id/ly_top_bar"

android:layout_width="match_parent"

android:layout_height="48dp"

android:visibility="gone">

</RelativeLayout>

<LinearLayout

android:id="@+id/ly_tab_bar"

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_alignParentBottom="true"

android:background="#FFFFFF"

android:orientation="vertical">

<View

android:layout_width="match_parent"

android:layout_height="2px"

android:background="#cccccc"

/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="40dp"

android:orientation="horizontal"

android:layout_marginTop="5dp">

<ImageView

android:id="@+id/iv_home"

android:layout_width="25.6dp"

android:layout_height="37.6dp"

android:src="@drawable/icon_home1"

android:layout_weight="1"/>

<ImageView

android:id="@+id/iv_query"

android:layout_width="25.6dp"

android:layout_height="37.6dp"

android:src="@drawable/icon_query1"

android:layout_weight="1"/>

<ImageView

android:id="@+id/iv_label"

android:layout_width="25.6dp"

android:layout_height="37.6dp"

android:src="@drawable/icon_label1"

android:layout_weight="1"/>

<ImageView

android:id="@+id/iv_mine"

android:layout_width="25.6dp"

android:layout_height="37.6dp"

android:src="@drawable/icon_mine1"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

<View

android:id="@+id/div_tab_bar"

android:layout_width="match_parent"

android:layout_height="2px"

android:background="#FFFFFF"

android:layout_above="@id/ly_tab_bar"

/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/fy_home"

android:layout_below="@id/ly_top_bar"

android:layout_above="@id/div_tab_bar"

/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/fy_query"

android:layout_below="@id/ly_top_bar"

android:layout_above="@id/div_tab_bar"/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/fy_label"

android:layout_below="@id/ly_top_bar"

android:layout_above="@id/div_tab_bar"/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/fy_mine"

android:layout_below="@id/ly_top_bar"

android:layout_above="@id/div_tab_bar"/>

</RelativeLayout>

</LinearLayout>

</android.support.v4.widget.DrawerLayout>第三步:在value文件下创建Style,并且自定义BaseAppTheme样式<?xml

version="1.0"

encoding="utf-8"

?>

<resources>

<color

name="primary">#1e89e7</color>

<color

name="primaryDark">#1976d2</color>

<color

name="red">#ff0000</color>

<color

name="white">#ffffff</color>

<style

name="BaseAppTheme"

parent="Theme.AppCompat.Light.DarkActionBar">

<item

name="windowActionBar">false</item>

<item

name="windowNoTitle">true</item>

<item

name="colorPrimary">@color/primary</item>

<item

name="colorPrimaryDark">@color/primaryDark</item>

<item

name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>

</style>

<style

name="AppTheme.DrawerArrowToggle"

parent="Base.Widget.AppCompat.DrawerArrowToggle">

<item

name="color">@android:color/white</item>

</style>

</resources>第四步:编写每个Fragment的后台,这里只写一个。using

System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

Android.App;

using

Android.Content;

using

Android.OS;

using

Android.Runtime;

using

Android.Util;

using

Android.Views;

using

Android.Widget;

namespace

BottomMuneDemo.Fragments

{

public

class

HomeFragment

:

Fragment

{

private

string

content

{

get;

set;

}

public

HomeFragment(string

content)

{

this.content

=

content;

}

public

override

void

OnCreate(Bundle

savedInstanceState)

{

base.OnCreate(savedInstanceState);

//

Create

your

fragment

here

}

public

override

View

OnCreateView(LayoutInflater

inflater,

ViewGroup

container,

Bundle

savedInstanceState)

{

View

view

=

inflater.Inflate(Resource.Layout.fg_home,

container,

false);

TextView

txt_content

=

(TextView)view.FindViewById(Resource.Id.txt_content);

txt_content.Text

=

"首页";

return

view;

}

}

}第五步:在Main活动中进行设置。using

Android.App;

using

Android.Widget;

using

Android.OS;

using

Android.Support.V7.App;

using

BottomMuneDemo.Fragments;

using

Android.Views;

namespace

BottomMuneDemo

{

[Activity(Label

=

"BottomMuneDemo",

MainLauncher

=

true,

Theme

=

"@style/BaseAppTheme")]

public

class

MainActivity

:

AppCompatActivity

{

private

ImageView

iv_home;

private

ImageView

iv_query;

private

ImageView

iv_label;

private

ImageView

iv_mine;

private

FrameLayout

fy_home;

private

FrameLayout

fy_query;

private

FrameLayout

fy_label;

private

FrameLayout

fy_mine;

HomeFragment

fg1;

QueryFragment

fg2;

LabelFragment

fg3;

MineFragment

fg4;

protected

override

void

OnCreate(Bundle

savedInstanceState)

{

base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.Main);

fy_home

=

(FrameLayout)FindViewById(Resource.Id.fy_home);

fy_query

=

(FrameLayout)FindViewById(Resource.Id.fy_query);

fy_label

=

(FrameLayout)FindViewById(Resource.Id.fy_label);

fy_mine

=

(FrameLayout)FindViewById(Resource.Id.fy_mine);

iv_home

=

(ImageView)FindViewById(Resource.Id.iv_home);

iv_query

=

(ImageView)FindViewById(Resource.Id.iv_query);

iv_label

=

(ImageView)FindViewById(Resource.Id.iv_label);

iv_mine

=

(ImageView)FindViewById(Resource.Id.iv_mine);

bindViews();

iv_home.PerformClick();

}

#region

底部菜单选项卡

//ui组件初始化与事件绑定

private

void

bindViews()

{

iv_home.Click

+=

(s,

e)

=>

{

onClick(iv_home);

};

iv_query.Click

+=

delegate

{

onClick(iv_query);

};

iv_label.Click

+=

delegate

{

onClick(iv_label);

};

iv_mine.Click

+=

delegate

{

onClick(iv_mine);

};

}

//隐藏所有Fragment

private

void

hideAllFragment(FragmentTransaction

fragmentTransaction)

{

if

(fg1

!=

null)

fragmentTransaction.Hide(fg1);

if

(fg2

!=

null)

fragmentTransaction.Hide(fg2);

if

(fg3

!=

null)

fragmentTransaction.Hide(fg3);

if

(fg4

!=

null)

fragmentTransaction.Hide(fg4);

iv_home.SetImageResource(Resource.Drawable.icon_home1);

iv_query.SetImageResource(Resource.Drawable.icon_query1);

iv_label.SetImageResource(Resource.Drawable.icon_label1);

iv_mine.SetImageResource(Resource.Drawable.icon_mine1);

}

//重置所有文本的选中状态

private

void

setSelected()

{

iv_home.Selected

=

false;

iv_query.Selected

=

false;

iv_label.Selected

=

false;

iv_mine.Selected

=

false;

}

//单击事件

public

void

onClick(View

v)

{

FragmentTransaction

fTransaction

=

FragmentManager.BeginTransaction();

hideAllFragment(fTransaction);

switch

(v.Id)

{

case

Resource.Id.iv_home:

setSelected();

iv_home.Selected

=

true;

iv_home.SetImageResource(Resource.Drawable.icon_home2);

if

(fg1

==

null)

{

fg1

=

new

HomeFragment("首页");

fTransaction.Add(Resource.Id.fy_home,

fg1);

}

else

{

fTransaction.Show(fg1);

}

break;

case

Resource.Id.iv_query:

setSelected();

iv_query.Sele

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论