Android 使用BottomNavigationView實現底部導航欄
今天我們來使用BottomNavigationView來實現android底部導航欄,在Android Support Library 25 中增加了 BottomNavigationView 控制元件,官方為我們提供了這樣這一個控制元件,就來試試吧!
1.效果圖如下:
2.匯入以下support:design library,BottomNavigationView就在這個design庫中。
compile'com.android.support:design:25.0.1'
3.在res下新建一個menu,然後在menu下建一個navigation.xml(在四個item中,放置了四個icon)。
navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/iv1_p"
android:title="首頁" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/iv2_p"
android:title="錢包" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/iv3_p"
android:title="卡片" />
<item
android:id="@+id/navigation_person"
android:icon="@drawable/iv4_p"
android:title="個人" />
</menu>
4.在佈局檔案中使用BottomNavigationView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.afanbaby.bottomnavigationdemo.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="@null"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
app:menu="@menu/navigation" />
</LinearLayout>
5.在drawable中新建bottom_navigation_selector.xml(設定文字和icon選中和未選中的顏色):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/tab_checked" android:state_checked="true" />
<item android:color="@color/tab_unchecked" android:state_checked="false" />
</selector>
6.color中增加兩個顏色值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tab_checked">#ED6A2C</color>
<color name="tab_unchecked">#757575</color>
</resources>
7.就可以在MainActivity中使用了:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private ViewPagerAdapter viewPagerAdapter;
private ViewPager viewPager;
private MenuItem menuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
viewPager = (ViewPager) findViewById(R.id.vp);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (menuItem != null) {
menuItem.setChecked(false);
} else {
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
menuItem = bottomNavigationView.getMenu().getItem(position);
menuItem.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
List<Fragment> list = new ArrayList<>();
list.add(TestFragment.newInstance("首頁"));
list.add(TestFragment.newInstance("錢包"));
list.add(TestFragment.newInstance("卡片"));
list.add(TestFragment.newInstance("個人"));
viewPagerAdapter.setList(list);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
menuItem = item;
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
viewPager.setCurrentItem(2);
return true;
case R.id.navigation_person:
viewPager.setCurrentItem(3);
return true;
}
return false;
}
};
}
8.ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public void setList(List<Fragment> list) {
this.list = list;
notifyDataSetChanged();
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list != null ? list.size() : 0;
}
}
9.測試的TestFragment:
public class TestFragment extends Fragment {
private TextView tv;
public static TestFragment newInstance(String name) {
Bundle args = new Bundle();
args.putString("name", name);
TestFragment fragment = new TestFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.fragment_test_tv);
Bundle bundle = getArguments();
if (bundle != null) {
String name = bundle.get("name").toString();
tv.setText(name);
}
}
}
10.測試的TestFragment的佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/fragment_test_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28dp"
android:gravity="center"
android:text="ceshi"/>
</LinearLayout>
這樣就完成了,是不是很簡單,快去試下吧!
11.需要注意的地方:
(1)取消位移動畫,如果你的選單數大於3個,則介面是這樣的(引用網路)。
這個效果可定不是我們想要的效果,可以通過反射解決。
新建一個BottomNavigationViewHelper.class:
public class BottomNavigationViewHelper {
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
(2)取消導航欄的點選效果(類似水波紋的效果)
我們只需在BottomNavigationView的佈局檔案中新增一個屬性:
app:itemBackground="@null"
- (3)取消導航欄的每項點選文字和圖片放大的效果
我們需要在values中的demens.xml中設定:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--BottomNavigationView 的選中沒有選中的字型大小-->
<dimen name="design_bottom_navigation_active_text_size">10dp</dimen>
<dimen name="design_bottom_navigation_text_size">10dp</dimen>
<!--BottomNavigationView 只放圖示時的設定-->
<!--<dimen name="design_bottom_navigation_active_text_size">0dp</dimen>-->
<!--<dimen name="design_bottom_navigation_text_size">0dp</dimen>-->
<!--<dimen name="design_bottom_navigation_margin">16dp</dimen>-->
</resources>
12.demo的地址:
相關文章
- Flutter仿閒魚底部導航欄實現Flutter
- [譯]在底部導航欄裡使用 ProviderIDE
- 快速實現底部導航欄及未讀訊息
- 底部導航欄懸浮效果
- 直播系統搭建,Android使用RadioGroup+RadioButton實現導航欄Android
- 直播網站原始碼,寫一個android底部導航欄框架網站原始碼Android框架
- 僅2步實現 拜拜 漢堡導航欄效果~ 全新底部導航互動(滑動隱藏)
- 定製化你的ReactNative底部導航欄React
- 解決使用BottomSheetDialog底部虛擬導航欄NavigationBar的顏色Navigation
- 直播小程式原始碼,配置tabbar底部導航欄原始碼tabBar
- vue2.0實現底部導航切換效果Vue
- android 自定義狀態列和導航欄分析與實現Android
- Android底部導航欄隨意定製加號、紅點、數字等資訊Android
- flutter 自定義tab導航-頂部導航-底部導航Flutter
- jQuery實現吸頂動畫導航欄jQuery動畫
- Flutter 底部導航詳解Flutter
- 直播系統程式碼,flutter手寫一個底部導航欄Flutter
- 直播app原始碼,全屏並且去掉底部虛擬導航欄APP原始碼
- Bootstrap 4 實現導航欄右側對齊boot
- 【前端】javascript實現導航欄筋斗雲效果特效前端JavaScript特效
- 成品直播原始碼推薦,uni底部導航欄隱藏單個原始碼
- 直播平臺原始碼,css移動端設定底部導航欄原始碼CSS
- Android全面屏虛擬導航欄適配Android
- 在 Flutter 中實現一個浮動導航欄Flutter
- 短視訊平臺原始碼,構建簡單的底部導航欄原始碼
- 移動端底部導航固定配合vue-router實現元件切換Vue元件
- 安卓導航抽屜 Navigation Drawer 實現沉浸通知欄安卓Navigation
- 手機直播原始碼,突出底部導航欄中間按鈕的樣式原始碼
- 微信小程式從零開始開發步驟(三)底部導航欄微信小程式
- Flutter 導航欄AppBarFlutterAPP
- Android 顯示、隱藏狀態列和導航欄Android
- JavaScript實現HTML導航欄下拉選單[懸浮顯示]JavaScriptHTML
- iOS 記一次導航欄平滑過渡的實現iOS
- qml 導航欄TabBar 工具欄ToolBartabBar
- Flutter 底部不規則導航製作Flutter
- 小程式底部導航tabBar不顯示tabBar
- Flutter 底部導航——BottomNavigationBar | 掘金技術徵文FlutterNavigation
- [譯] 如何用 Flutter 來建立一個帶有底部導航欄的應用程式Flutter