今天主要完成的任务:简单底部导航栏的设计
遇到的困难:自己的sdk版本比较新,而参考的底部导航栏的代码过老,导致了底部导航栏的效果很差
解决办法:换了一种比较新的底部导航栏的设计思路
意外的惊喜:bufferknife框架
今天令我感慨最深的是:明明羡慕bufferknife框架的简单,可自己偏偏不会配置,可以说今天大部分的时间都浪费在配置bufferknife框架上了吧,查阅了大部分网上的资料基本上都是错的,可能由于年代比较久远吧。最终依靠github上作者的说明得以解决,其实github上对该框架如何使用已经做了详细的说明还有demo可以参考,只是自己还是太懒,不肯费力去阅读,自己以后需要加强对代码的阅读和理解能力,学会利用GitHub上的资源。
源程序代码
main_bottom.xml:
<?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="55dp"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/title_text"> </View> <LinearLayout android:layout_width="match_parent" android:layout_height="54dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/ll_main_record" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_main_record" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginTop="2dp" android:src="@drawable/jilu" /> <TextView android:id="@+id/tv_main_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="记录" android:textSize="16sp" android:textColor="@color/home_back_unselected" /> </LinearLayout> <LinearLayout android:id="@+id/ll_main_note" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_main_note" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginTop="2dp" android:src="@drawable/biji" /> <TextView android:id="@+id/tv_main_note" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="笔记" android:textSize="16sp" android:textColor="@color/home_back_unselected" /> </LinearLayout> <LinearLayout android:id="@+id/ll_main_space" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_main_space" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginTop="2dp" android:src="@drawable/space" /> <TextView android:id="@+id/tv_main_space" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="尚记堂" android:textColor="@color/home_back_unselected" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_main_mine" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_main_mine" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginTop="2dp" android:src="@drawable/wode" /> <TextView android:id="@+id/tv_main_mine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="我的" android:textColor="@color/home_back_unselected" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </LinearLayout>
activity_main.xml
<?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" tools:context=".activity.MainActivity"> <FrameLayout android:id="@+id/fl_main" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <include layout="@layout/main_bottom"></include> </LinearLayout>
MainActivty.java
package com.itheima.network.activity; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import com.itheima.network.R; import com.itheima.network.fragment.Mine; import com.itheima.network.fragment.Note; import com.itheima.network.fragment.Notespace; import com.itheima.network.fragment.Record; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends FragmentActivity { private FragmentTransaction transaction; @BindView(R.id.fl_main) FrameLayout flMain; @BindView(R.id.iv_main_record) ImageView ivMainRecord; @BindView(R.id.tv_main_record) TextView tvMainRecord; @BindView(R.id.ll_main_record) LinearLayout llMainRecord; @BindView(R.id.iv_main_note) ImageView ivMainNote; @BindView(R.id.tv_main_note) TextView tvMainNote; @BindView(R.id.ll_main_note) LinearLayout llMainNote; @BindView(R.id.iv_main_space) ImageView ivMainSpace; @BindView(R.id.tv_main_space) TextView tvMainSpace; @BindView(R.id.ll_main_space) LinearLayout llMainSpace; @BindView(R.id.iv_main_mine) ImageView ivMainMine; @BindView(R.id.tv_main_mine) TextView tvMainMine; @BindView(R.id.ll_main_mine) LinearLayout llMainMine; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //默认加载 setSlect(0); } @OnClick({R.id.ll_main_record,R.id.ll_main_note,R.id.ll_main_space,R.id.ll_main_mine}) public void showTab(View view) { switch (view.getId()) { case R.id.ll_main_record: setSlect(0); break; case R.id.ll_main_note: setSlect(1); break; case R.id.ll_main_space: setSlect(2); break; case R.id.ll_main_mine: setSlect(3); break; } } private Record record; private Note note; private Notespace notespace; private Mine mine; //提供相应的fragment显示 private void setSlect(int i) { FragmentManager fragmentManager=this.getSupportFragmentManager(); transaction=fragmentManager.beginTransaction(); //隐藏fragment hideFragments(); //重置颜色 resetTab(); switch (i) { case 0: if(record==null) { record=new Record(); transaction.add(R.id.fl_main,record); } transaction.show(record); ivMainRecord.setImageResource(R.drawable.jilu1); tvMainRecord.setTextColor(getResources().getColor(R.color.home_back_selected)); break; case 1: if(note==null) { note=new Note(); transaction.add(R.id.fl_main,note); } transaction.show(note); ivMainNote.setImageResource(R.drawable.biji1); tvMainNote.setTextColor(getResources().getColor(R.color.home_back_selected)); break; case 2: if(notespace==null) { notespace=new Notespace(); transaction.add(R.id.fl_main,notespace); } transaction.show(notespace); ivMainSpace.setImageResource(R.drawable.space1); tvMainSpace.setTextColor(getResources().getColor(R.color.home_back_selected)); break; case 3: if(mine==null) { mine=new Mine(); transaction.add(R.id.fl_main,mine); } transaction.show(mine); ivMainMine.setImageResource(R.drawable.wode1); tvMainMine.setTextColor(getResources().getColor(R.color.home_back_selected)); break; } transaction.commit(); } private void resetTab() { ivMainRecord.setImageResource(R.drawable.jilu); ivMainNote.setImageResource(R.drawable.biji); ivMainSpace.setImageResource(R.drawable.space); ivMainMine.setImageResource(R.drawable.wode); tvMainNote.setTextColor(getResources().getColor(R.color.home_back_unselected)); tvMainNote.setTextColor(getResources().getColor(R.color.home_back_unselected)); tvMainSpace.setTextColor(getResources().getColor(R.color.home_back_unselected)); tvMainMine.setTextColor(getResources().getColor(R.color.home_back_unselected)); } private void hideFragments() { if(record!=null) { transaction.hide(record); } if(note!=null) { transaction.hide(note); } if(notespace!=null) { transaction.hide(notespace); } if(mine!=null) { transaction.hide(mine); } } }
运行效果截图