底部導航欄實現一 Fragment-replace

来源:https://www.cnblogs.com/xqz0618/archive/2018/04/21/nav1.html
-Advertisement-
Play Games

【效果】(這裡下載的軟體收費的試用有水印) 【推薦】這裡推薦一個圖標網http://iconfont.cn/。以上圖標來自此圖標網 【項目結構】 【步驟】 ①創建佈局文件,寫底部導航欄 ②定義Fragment 【提示】可以通過下圖方式創建 對於生成的Fragment不用作修改,對應的佈局中設置一個背 ...


【效果】(這裡下載的軟體收費的試用有水印)

                   

 【推薦】這裡推薦一個圖標網http://iconfont.cn/。以上圖標來自此圖標網

【項目結構】

                   

 

【步驟】

①創建佈局文件,寫底部導航欄

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:app="http://schemas.android.com/apk/res-auto"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     tools:context=".MainActivity"
  8     android:orientation="vertical">
  9 
 10     <RelativeLayout
 11         android:id="@+id/rl_content"
 12         android:layout_width="match_parent"
 13         android:layout_height="0dp"
 14         android:layout_weight="1">
 15        <!--這裡設置權重weight為1, 下麵不設置權重。-->
 16         <!--意思是,剩餘的位置全都是RelativeLayout的-->
 17     </RelativeLayout>
 18 
 19     <TextView
 20         android:layout_width="match_parent"
 21         android:layout_height="1dp"
 22         android:background="#797878"/>
 23     <LinearLayout
 24         android:layout_width="match_parent"
 25         android:layout_height="80dp"
 26         android:orientation="horizontal">
 27         <LinearLayout
 28             android:id="@+id/item1"
 29             android:layout_width="0dp"
 30             android:layout_weight="1"
 31             android:layout_height="match_parent"
 32             android:orientation="vertical">
 33             <ImageView
 34                 android:id="@+id/item1_iv"
 35                 android:layout_width="match_parent"
 36                 android:layout_height="0dp"
 37                 android:layout_weight="3"
 38                 android:layout_margin="3dp"
 39                 android:scaleType="fitCenter"
 40                 android:src="@drawable/wxb"
 41                 android:padding="1dp"/>
 42             <TextView
 43                 android:id="@+id/item1_tv"
 44                 android:text="女王"
 45                 android:textSize="16sp"
 46                 android:layout_width="match_parent"
 47                 android:layout_height="0dp"
 48                 android:layout_weight="1"
 49                 android:gravity="center"/>
 50         </LinearLayout>
 51         <LinearLayout
 52             android:id="@+id/item2"
 53             android:layout_width="0dp"
 54             android:layout_weight="1"
 55             android:layout_height="match_parent"
 56             android:orientation="vertical">
 57             <ImageView
 58                 android:id="@+id/item2_iv"
 59                 android:layout_width="match_parent"
 60                 android:layout_height="0dp"
 61                 android:layout_weight="3"
 62                 android:layout_margin="3dp"
 63                 android:scaleType="fitCenter"
 64                 android:src="@drawable/meizhuang"
 65                 android:padding="4dp"/>
 66             <TextView
 67                 android:id="@+id/item2_tv"
 68                 android:text="美妝"
 69                 android:textSize="16sp"
 70                 android:layout_width="match_parent"
 71                 android:layout_height="0dp"
 72                 android:layout_weight="1"
 73                 android:gravity="center"/>
 74         </LinearLayout>
 75         <LinearLayout
 76             android:id="@+id/item3"
 77             android:layout_width="0dp"
 78             android:layout_weight="1"
 79             android:layout_height="match_parent"
 80             android:orientation="vertical">
 81             <ImageView
 82                 android:id="@+id/item3_iv"
 83                 android:layout_width="match_parent"
 84                 android:layout_height="0dp"
 85                 android:layout_weight="3"
 86                 android:layout_margin="3dp"
 87                 android:scaleType="fitCenter"
 88                 android:src="@drawable/fuzhuang"
 89                 android:padding="5dp"/>
 90             <TextView
 91                 android:id="@+id/item3_tv"
 92                 android:text="衣帽"
 93                 android:textSize="16sp"
 94                 android:layout_width="match_parent"
 95                 android:layout_height="0dp"
 96                 android:layout_weight="1"
 97                 android:gravity="center" />
 98         </LinearLayout>
 99         <LinearLayout
100             android:id="@+id/item4"
101             android:layout_width="0dp"
102             android:layout_weight="1"
103             android:layout_height="match_parent"
104             android:orientation="vertical">
105             <ImageView
106                 android:id="@+id/item4_iv"
107                 android:layout_width="match_parent"
108                 android:layout_height="0dp"
109                 android:layout_weight="3"
110                 android:layout_margin="3dp"
111                 android:scaleType="fitCenter"
112                 android:src="@drawable/xiebaopeishi"
113                 android:padding="3dp"/>
114             <TextView
115                 android:id="@+id/item4_tv"
116                 android:text="鞋包"
117                 android:textSize="16sp"
118                 android:layout_width="match_parent"
119                 android:layout_height="0dp"
120                 android:layout_weight="1"
121                 android:gravity="center" />
122         </LinearLayout>
123     </LinearLayout>
124 </LinearLayout>

 

②定義Fragment

  【提示】可以通過下圖方式創建

 

 1 public class FragmentA extends Fragment {
 2     
 3     public FragmentA() {
 4         // Required empty public constructor
 5     }
 6 
 7     @Override
 8     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 9                              Bundle savedInstanceState) {
10         // Inflate the layout for this fragment
11         return inflater.inflate(R.layout.fragment_a, container, false);
12     }
13 }

對於生成的Fragment不用作修改,對應的佈局中設置一個背景顏色便於觀察。

 

③MainActivity代碼的編寫

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 2 
 3     private FragmentManager fragmentManager;
 4     private RelativeLayout rl_content;
 5     private ImageView item1_iv,item2_iv,item3_iv,item4_iv;
 6     private TextView item1_tv,item2_tv,item3_tv,item4_tv;
 7     private LinearLayout item1,item2,item3,item4;
 8     private ImageView[] ivs;
 9     private TextView[] tvs;
10     
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15 
16         initView();
17 
18         fragmentManager = getSupportFragmentManager();
19 
20         initListener();
21     }
22 
23     private void initListener() {
24         item1.setOnClickListener(this);
25         item2.setOnClickListener(this);
26         item3.setOnClickListener(this);
27         item4.setOnClickListener(this);
28     }
29 
30     private void initView() {
31         rl_content = (RelativeLayout) findViewById(R.id.rl_content);
32         item1_iv = (ImageView) findViewById(R.id.item1_iv);
33         item1_tv = (TextView) findViewById(R.id.item1_tv);
34         item1 = (LinearLayout) findViewById(R.id.item1);
35         item2_iv = (ImageView) findViewById(R.id.item2_iv);
36         item2_tv = (TextView) findViewById(R.id.item2_tv);
37         item2 = (LinearLayout) findViewById(R.id.item2);
38         item3_iv = (ImageView) findViewById(R.id.item3_iv);
39         item3_tv = (TextView) findViewById(R.id.item3_tv);
40         item3 = (LinearLayout) findViewById(R.id.item3);
41         item4_iv = (ImageView) findViewById(R.id.item4_iv);
42         item4_tv = (TextView) findViewById(R.id.item4_tv);
43         item4 = (LinearLayout) findViewById(R.id.item4);
44         ivs = new ImageView[]{item1_iv,item2_iv,item3_iv,item4_iv};
45         tvs = new TextView[]{item1_tv,item2_tv,item3_tv,item4_tv};
46     }
47 
48     @Override
49     public void onClick(View view) {
50         switch (view.getId()){
51             case R.id.item1: {
52                 FragmentTransaction transaction = fragmentManager.beginTransaction();//創建一個事務
53                 transaction.replace(R.id.rl_content,new FragmentA());
54                 transaction.commit();//事務一定要提交,replace才會有效
55                 setCheck(0);//自定義方法
56                 break;
57             }
58             case R.id.item2: {
59                 FragmentTransaction transaction = fragmentManager.beginTransaction();
60                 transaction.replace(R.id.rl_content,new FragmentB());
61                 transaction.commit();
62                 setCheck(1);
63                 break;
64             }
65             case R.id.item3: {
66                 FragmentTransaction transaction = fragmentManager.beginTransaction();
67                 transaction.replace(R.id.rl_content,new FragmentC());
68                 transaction.commit();
69                 setCheck(2);
70                 break;
71             }
72             case R.id.item4: {
73                 FragmentTransaction transaction = fragmentManager.beginTransaction();
74                 transaction.replace(R.id.rl_content,new FragmentD());
75                 transaction.commit();
76                 setCheck(3);
77                 break;
78             }
79             default:break;
80         }
81     }
82 
83     public void setCheck(int itemId){
84         //這個方法設置底部導航欄選中時的效果
85         for (int i = 0; i < 4; i++) {
86             ivs[i].setColorFilter(Color.parseColor("#0f0f0f"));
87             tvs[i].setTextColor(Color.parseColor("#0f0f0f"));
88         }
89         ivs[itemId].setColorFilter(Color.GREEN);
90         tvs[itemId].setTextColor(Color.GREEN);
91     }
92 }

【提示】①這裡的點擊事件是通過Activity實現Onclick介面的方式

          ②getSupportFragmentManager()是v4包中的,相容效果好,如果用getFragmentManager()可能會崩掉

          ③FragmentManager只需要獲取一次,但是事務FragmentTransaction要重新開啟。最後事務一定要提交commit。

          ④方法setCheck是為了設置導航的被選中效果。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在linux系統使用中,掌握熟練的vi編輯器,可以提高linux工作效率。那麼vi編輯器的使用方法有哪些呢? vi編輯器可在絕大部分linux發行版中使用。 Vi編輯器的作用:創建或修改文件;維護linux系統中的各種配置文件。 vi沒有菜單,只有命令 vi有三種基本工作模式,分別是:命令模式(co ...
  • 1.進入root su 2.編輯文件 vim /etc/inputrc [按下i鍵開始輸入] 3.加入新配置 set completion-ignore-case on 4.保存 [按下esc鍵,再輸入:wq確定保存] 5.重啟 reboot ...
  • 官網地址:https://redis.io/download 最新版本是4.0,在這裡本人下的是3.2 使用rz命令可以將Redis上傳到Linux系統 首先要確定Linux上是否安裝了gcc,沒有則使用yum install gcc-c++安裝 在安裝時出現下麵的錯誤提示 使用rm -f /var ...
  • 經常玩伺服器上的mysql資料庫,但是基於linux操作Mysql多有不便,於是就想著使用GUI工具來遠程操作mysql資料庫。已經不是三次使用navicat-for-mysql了,但是每次連接遠程伺服器的Mysql資料庫都會遇到問題,煩,索性這次就記下來,下次再使用navicat for mysq ...
  • SQL語句的DECODE()和NVL()函數用法 SELECT DECODE(choose_tool,0,'寶馬',1,'電動車',2,'自行車','步行') AS my_tool FROM data_tool_t //分析:當choose_tool為0時候,my_tool=‘寶馬’ 當choose ...
  • 1 -- 1、DECLARE EXIT HANDLER FOR SQLEXCEPTION 語句後面可以跟一個 begin end的複合語句塊,也可以直接跟一個簡單語句例如 :DECLARE EXIT HANDLER FOR SQLEXCEPTION v_succ=0; 3 -- 2、EXIT會在執行... ...
  • 一、Android studio的安裝 【提示】A、以下Android studio2.2.2版本。(也有新版本) B、以下是用Android studio自帶的sdk ①雙擊安裝文件進行安裝 ②如果沒有SDK,要勾選Android SDK。(如果有可以選擇不勾選) ③設置Android studi ...
  • 相信大部分人都被Android Studio環境配置搞得很頭痛,至少我為這個配置花費了將近5h,Android Studio作為一款強大的IDE,比起Eclipse來說,更加方便Android的開發,但作為新手的我們不應該因為Android Studio的配置而打退堂鼓,這篇文章儘量從配置原理方面來... ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...