原文出处

标题:LinearLayout+Fragment实现下导航栏效果
作者:天地炫舞
原文链接:https://blog.csdn.net/tiandixuanwuliang/article/details/56279956

前言

本文将利用LinearLayout+Fragment实现下导航栏效果
手机APP开发者可以直接在本测试案例上进行二次开发,可以节省自己手动写导航栏的时间和精力。

实现原理:

先使用在MainActivity中使用LinearLayout做出下导航栏的布局效果;

再实现各个按钮的点击事件,替换掉MainActivity对应的xml文件中的Fragment;

优化部分:1、页面一加载时就要替换index的fragment;2、按钮被点击了,在xml文件中改变当前按钮的状态。

效果图如下:
image.png
下面开始介绍本测试案例。

一、以下是项目工程截图

image.png

二、com.wllfengshu.share中的MainActivity.java为程序入口

java代码如下

  1. package com.wllfengshu.share;
  2. import com.wllfengshu.donate.DonateFragment;
  3. import com.wllfengshu.donate.R;
  4. import com.wllfengshu.index.IndexFragment;
  5. import com.wllfengshu.me.MeFragment;
  6. import com.wllfengshu.query.QueryFragment;
  7. import android.app.Activity;
  8. import android.app.FragmentManager;
  9. import android.app.FragmentTransaction;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.view.Window;
  14. import android.widget.ImageView;
  15. import android.widget.LinearLayout;
  16. import android.widget.TextView;
  17. public class MainActivity extends Activity implements OnClickListener {
  18. private LinearLayout llIndex, llDonate, llQuery, llMe;
  19. private ImageView ivIndex, ivDonate, ivQuery, ivMe, ivCurrent;
  20. private TextView tvIndex, tvDonate, tvQuery, tvMe, tvCurrent;
  21. private FragmentManager fragmentManager;
  22. private FragmentTransaction beginTransaction;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. requestWindowFeature(Window.FEATURE_NO_TITLE);
  27. setContentView(R.layout.activity_main);
  28. // 浠ヤ笅鏄垵濮嬪寲start
  29. llIndex = (LinearLayout) findViewById(R.id.llIndex);
  30. llDonate = (LinearLayout) findViewById(R.id.llDonate);
  31. llQuery = (LinearLayout) findViewById(R.id.llQuery);
  32. llMe = (LinearLayout) findViewById(R.id.llMe);
  33. llIndex.setOnClickListener(this);
  34. llDonate.setOnClickListener(this);
  35. llQuery.setOnClickListener(this);
  36. llMe.setOnClickListener(this);
  37. ivIndex = (ImageView) findViewById(R.id.ivIndex);
  38. ivDonate = (ImageView) findViewById(R.id.ivDonate);
  39. ivQuery = (ImageView) findViewById(R.id.ivQuery);
  40. ivMe = (ImageView) findViewById(R.id.ivMe);
  41. tvIndex = (TextView) findViewById(R.id.tvIndex);
  42. tvDonate = (TextView) findViewById(R.id.tvDonate);
  43. tvQuery = (TextView) findViewById(R.id.tvQuery);
  44. tvMe = (TextView) findViewById(R.id.tvMe);
  45. ivIndex.setSelected(true);
  46. tvIndex.setSelected(true);
  47. ivCurrent = ivIndex;
  48. tvCurrent = tvIndex;
  49. // 浠ヤ笅鏄垵濮嬪寲end
  50. fragmentManager = getFragmentManager();
  51. beginTransaction = fragmentManager.beginTransaction();
  52. beginTransaction.replace(R.id.ll_main, new IndexFragment());
  53. beginTransaction.commit();
  54. }
  55. @Override
  56. public void onClick(View v) {
  57. ivCurrent.setSelected(false);
  58. tvCurrent.setSelected(false);
  59. FragmentManager fragmentManager = getFragmentManager();
  60. FragmentTransaction beginTransaction = fragmentManager
  61. .beginTransaction();
  62. switch (v.getId()) {
  63. case R.id.llIndex:
  64. beginTransaction.replace(R.id.ll_main, new IndexFragment());
  65. case 0:
  66. ivIndex.setSelected(true);
  67. ivCurrent = ivIndex;
  68. tvIndex.setSelected(true);
  69. tvCurrent = tvIndex;
  70. break;
  71. case R.id.llDonate:
  72. beginTransaction.replace(R.id.ll_main, new DonateFragment());
  73. case 1:
  74. ivDonate.setSelected(true);
  75. ivCurrent = ivDonate;
  76. tvDonate.setSelected(true);
  77. tvCurrent = tvDonate;
  78. break;
  79. case R.id.llQuery:
  80. beginTransaction.replace(R.id.ll_main, new QueryFragment());
  81. case 2:
  82. ivQuery.setSelected(true);
  83. ivCurrent = ivQuery;
  84. tvQuery.setSelected(true);
  85. tvCurrent = tvQuery;
  86. break;
  87. case R.id.llMe:
  88. beginTransaction.replace(R.id.ll_main, new MeFragment());
  89. case 3:
  90. ivMe.setSelected(true);
  91. ivCurrent = ivMe;
  92. tvMe.setSelected(true);
  93. tvCurrent = tvMe;
  94. break;
  95. default:
  96. break;
  97. }
  98. beginTransaction.commit();
  99. }
  100. }

其对应的xml文件代码如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="45dp"
  9. android:background="#0E6DB0"
  10. android:gravity="center"
  11. android:orientation="vertical" >
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center"
  16. android:text="@string/app_name"
  17. android:textColor="#ffffff"
  18. android:textSize="20sp"
  19. android:textStyle="bold" />
  20. </LinearLayout>
  21. <LinearLayout
  22. android:id="@+id/ll_main"
  23. android:layout_width="match_parent"
  24. android:layout_height="0dp"
  25. android:layout_weight="1"
  26. android:orientation="vertical" >
  27. </LinearLayout>
  28. <LinearLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="55dp"
  31. android:background="#0E6DB0"
  32. android:orientation="horizontal" >
  33. <LinearLayout
  34. android:id="@+id/llIndex"
  35. android:layout_width="0dp"
  36. android:layout_height="match_parent"
  37. android:layout_weight="1"
  38. android:gravity="center"
  39. android:orientation="vertical" >
  40. <ImageView
  41. android:id="@+id/ivIndex"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:background="#00000000"
  45. android:src="@drawable/tab_index" />
  46. <TextView
  47. android:id="@+id/tvIndex"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="首页"
  51. android:textColor="@drawable/tab_textview" />
  52. </LinearLayout>
  53. <LinearLayout
  54. android:id="@+id/llDonate"
  55. android:layout_width="0dp"
  56. android:layout_height="match_parent"
  57. android:layout_weight="1"
  58. android:gravity="center"
  59. android:orientation="vertical" >
  60. <ImageView
  61. android:id="@+id/ivDonate"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:background="#00000000"
  65. android:src="@drawable/tab_donate" />
  66. <TextView
  67. android:id="@+id/tvDonate"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:text="捐赠"
  71. android:textColor="@drawable/tab_textview" />
  72. </LinearLayout>
  73. <LinearLayout
  74. android:id="@+id/llQuery"
  75. android:layout_width="0dp"
  76. android:layout_height="match_parent"
  77. android:layout_weight="1"
  78. android:gravity="center"
  79. android:orientation="vertical" >
  80. <ImageView
  81. android:id="@+id/ivQuery"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:background="#00000000"
  85. android:src="@drawable/tab_query" />
  86. <TextView
  87. android:id="@+id/tvQuery"
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:text="查询"
  91. android:textColor="@drawable/tab_textview" />
  92. </LinearLayout>
  93. <LinearLayout
  94. android:id="@+id/llMe"
  95. android:layout_width="0dp"
  96. android:layout_height="match_parent"
  97. android:layout_weight="1"
  98. android:gravity="center"
  99. android:orientation="vertical" >
  100. <ImageView
  101. android:id="@+id/ivMe"
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:background="#00000000"
  105. android:src="@drawable/tab_me" />
  106. <TextView
  107. android:id="@+id/tvMe"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:text="我"
  111. android:textColor="@drawable/tab_textview" />
  112. </LinearLayout>
  113. </LinearLayout>
  114. </LinearLayout>

三、index页面中

java代码如下

  1. package com.wllfengshu.index;
  2. import com.wllfengshu.donate.R;
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. public class IndexFragment extends Fragment {
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState) {
  12. View view=inflater.inflate(R.layout.index_fragment, null);
  13. return view;
  14. }
  15. }

其对应的xml文件代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:text="我是index"/>
  9. </RelativeLayout>

四、其余几个页面中的java代码和xml代码和index页面一样,在此不再重复

说明:本文参考了一些导航栏制作方法,但此种方法代码量最少,最好理解,不需要导入其他jar包,此案例中的一些图片资源、布局等使用其他案例的资源。

源代码:链接:http://download.csdn.net/detail/tiandixuanwuliang/9766787