服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 详解Xamarin.Android 利用Fragment实现底部菜单

详解Xamarin.Android 利用Fragment实现底部菜单

2022-09-26 15:02键盘演绎青春 Android

这篇文章主要介绍了详解Xamarin.Android 利用Fragment实现底部菜单,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本篇文章主要介绍了详解xamarin.android 利用fragment实现底部菜单,分享给大家,具体如下:

效果图:

详解Xamarin.Android 利用Fragment实现底部菜单

第一步:添加引用

引用 crosslight.xamarin.android.support.v7.appcompat 这个包。

详解Xamarin.Android 利用Fragment实现底部菜单

第二步:绘制main和fragment界面

fg_home.axml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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: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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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: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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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: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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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: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

?
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
  <include
    layout="@layout/main_left" />
</linearlayout>

main_left.xml

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/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 样式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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的后台,这里只写一个。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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活动中进行设置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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.selected = true;
          iv_query.setimageresource(resource.drawable.icon_query2);
          if (fg2 == null)
          {
            fg2 = new queryfragment("查询");
            ftransaction.add(resource.id.fy_query, fg2);
          }
          else { ftransaction.show(fg2); }
          break;
 
        case resource.id.iv_label:
          setselected();
          iv_label.selected = true;
          iv_label.setimageresource(resource.drawable.icon_label2);
          if (fg3 == null)
          {
            fg3 = new labelfragment("贴签");
            ftransaction.add(resource.id.fy_label, fg3);
          }
          else { ftransaction.show(fg3); }
          break;
 
        case resource.id.iv_mine:
          setselected();
          iv_mine.selected = true;
          iv_mine.setimageresource(resource.drawable.icon_mine2);
          if (fg4 == null)
          {
            fg4 = new minefragment("我的");
            ftransaction.add(resource.id.fy_mine, fg4);
          }
          else { ftransaction.show(fg4); }
          break;
      }
      ftransaction.commit();
    }
    #endregion
  }
}

到这里就结束了,亲测代码有效,如有问题请留言。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/swjian/p/10409114.html

延伸 · 阅读

精彩推荐