In navigation_bar. XML, there is a navigation bar, and the
tag is used to import each Activity. Using the butter knife directly to bind the button ID in the navigation bar fails. The butter knife cannot be directly bound to the ID of the component in the
tag. You can set the ID of the
tag to bind to the Activity View: butterknife. bind(r.layout.activity_main, navigationView); navigation_bar.xml:
<? The 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="40dp" android:gravity="center" android:background="#3F4145" android:orientation="horizontal"> <ImageButton android:id="@+id/ib_nav_question" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/ic_nav_question" /> <ImageButton android:id="@+id/ib_nav_back" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/ic_nav_back" /> <ImageButton android:id="@+id/ib_nav_menu" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/ic_nav_menu" /> <Button android:id="@+id/bt_nav_page" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/ic_nav_page" android:text="2" android:textColor="#FFFFFF" android:textSize="10sp" /> <ImageButton android:id="@+id/ib_nav_refresh" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/ic_nav_refresh" /> </LinearLayout>Copy the code
MainActivity corresponds to the XML file r.layout.activity_main
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/navigation_bar"
android:id="@+id/navigation_bar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code
In the MainActivity:
Public class MainActivity extends BaseActivity {// Use the butter knife to bind the component include @bindView (R.id.navigation_bar) LinearLayout navigationView; private Unbinder unbinder; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); unbinder=ButterKnife.bind(R.layout.activity_main, navigationView); } @onclick ({r.i.ib_nav_question, R.I.ib_nav_back, R.I.ib_nav_menu, R.I.bt_nav_page, R.id.ib_nav_refresh}) public void onClick(View view) { switch (view.getId()) { case R.id.ib_nav_question: break; Case r.i.ib_nav_back: // Exit app setResult(activity.result_ok); finishAndRemoveTask(); break; Case R.i db t_nav_page: / / activity stack IntentUtil intentToWebAppTask (MainActivity. This, null); break; case R.id.ib_nav_menu: case R.id.ib_nav_refresh: break; } } @Override protected void onDestroy() { super.onDestroy(); Unbinder.unbind (); }}Copy the code