Encapsulation NavigationBar
Reason: Most of the time, the Activity used in our application needs to display different effects due to different interfaces. For example, the back button may need to appear. Maybe you need a personal center button and so on, so we wrap up the navigation so that when the external is used, the layout can be called directly, passing different parameters to achieve different interface effects.
steps
1. Create one in the Layout filenav_bar.xml
Layout file
<? The XML version = "1.0" encoding = "utf-8"? > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="@dimen/navBarHeight" android:background="@color/mainColor" android:paddingLeft="@dimen/marginSize" android:paddingRight="@dimen/marginSize"> <ImageView android:id="@+id/iv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/back" android:layout_gravity="center_vertical"/> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/navBarTitleSize" android:textColor="@android:color/white" Android :text=" music "Android :layout_gravity="center"/> <ImageView android:id="@+id/iv_me" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/me" android:layout_gravity="right|center_vertical"/> </FrameLayout>Copy the code
2. Pass in other XML layout files<include layout = ""/>
reference
<include layout="@layout/nav_bar" />
Copy the code
3. Encapsulate it in the parent class activity.java
/** * Initializes NavigationBar *@param isShowBack
* @param title
* @param isShowMe
*/
protected void initNavBar (boolean isShowBack, String title, boolean isShowMe){
mIvBack = fd(R.id.iv_back);
mTvTitle = fd(R.id.tv_title);
mIvMe = fd(R.id.iv_me);
mIvBack.setVisibility(isShowBack ? View.VISIBLE : View.GONE);
mTvTitle.setText(title);
mIvMe.setVisibility(isShowMe ? View.VISIBLE : View.GONE);
// Give the return key a click event
mIvBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { onBackPressed(); }}); mIvMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(BaseActivity.this,MeActivity.class)); }}); }Copy the code
4. Invoke and initialize the activity in use
/** * Initializes view */
private void initView (a){
initNavBar(false."Login".false);
mInputPhone = fd(R.id.input_phone);
mInputPassword = fd(R.id.input_password);
}
Copy the code