安卓開發實戰 大學班級APP開發 練手專案 (一)Android Studio 自帶模板Bottom Navigation Activity 的使用
阿新 • • 發佈:2018-12-18
一、建立Bottom Navigation
使用Android Studio建立工程,在選擇activity的時候選擇Bottom Navigation Activity
之後 xml檔案 程式碼修改如下
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/content" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="0dp" app:layout_constraintBottom_toTopOf="@+id/navigation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </FrameLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="0dp" android:layout_marginStart="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" /> </android.support.constraint.ConstraintLayout>
activity程式碼暫時不變
二、結合Fragment使用
首先我們建立空白的Fragment,如下圖。主頁要幾個頁面就建立幾個(注意,不能是任意個,主頁介面是3-5個)。建立完成之後呢,Fragment類裡面的程式碼很多,我們不需要這些,把他刪掉,只剩下這一個方法onCreateView這一個方法就行了。刪完後代碼如下,所有新增的Fragment類都是這樣。
package com.mobileshopdemo.qingyin.myapplicationclassdemo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 第一個頁面 課表查詢 * * @author qingyin * */ public class BlankFragment extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_blank, container, false); return view; } }
建立相對應的佈局檔案
修改MainActivity程式碼
package com.mobileshopdemo.qingyin.myapplicationclassdemo; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { private FragmentTransaction transaction; private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setDefaultFragment(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } // 設定預設tab顯示頁面 private void setDefaultFragment() { fragmentManager = getSupportFragmentManager(); transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.content, new BlankFragment()).commit(); } //設定3-5個碎片化介面 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { fragmentManager = getSupportFragmentManager(); transaction = fragmentManager.beginTransaction(); switch (item.getItemId()) { case R.id.navigation_home: transaction.replace(R.id.content, new BlankFragment()); transaction.commit(); return true; case R.id.navigation_dashboard: transaction.replace(R.id.content, new BlankFragment2()); transaction.commit(); return true; case R.id.navigation_notifications: transaction.replace(R.id.content, new BlankFragment3()); transaction.commit(); return true; } return false; } }; }
在Fragment裡面是無法直接findviewbyid找到控制元件的,必須要在前面加getView().或者view.才行。
注意一點,需要重寫onStart方法,所有的控制元件放到這裡面初始化。
@Override
public void onStart() {
TextView textView = getView().findViewById(R.id.textOne);
textView.setText("我是第一個檢視哦");
super.onStart();
}
需要新增或更改Bottom圖示和文字的需要在Navigation.xml裡面修改。