preface

Recently, I was wondering how to encapsulate the toolbars to make them more generic, easier to look at, and simpler to look at. I was thinking about rewriting the toolbars. Finally, I saw a lot of people using BaseActivity for toolbars It’s much easier to write a BaseActivity that inherits from your own Activity

For example

Because the amount of code is still very small usually like to use netease Cloud to imitate netease cloud bit by bit as a learning Demo, so here to achieve netease cloud music in several Tolbar

A few native screenshots

Code implementation

At the beginning of the project, there is always a BaseActivity to do some unified operations, and then all activities inherit uniformly.

Main code:

Public class BaseActivity extends AppActivity {// Generic Toolbar title private TextView commonTitleTv; // ToolBar Private ToolBar commonTitleTb; // Private FrameLayout content; Private ImageView img; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_base);
        initView();
        setSupportActionBar(commonTitleTb);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    private void initView(){ commonTitleTv = findViewById(R.id.commom_title); commonTitleTb = findViewById(R.id.toolbar); content = findViewById(R.id.content); img = findViewById(R.id.commom_img); } // Subclass call resets Toolbar public voidsetToolBar(int layout){
        hidetoolBar();
        commonTitleTb = content.findViewById(layout);
        setSupportActionBar(commonTitleTb); / / set the actionbar whether the title display corresponding actionbar. DISPLAY_SHOW_TITLE getSupportActionBar () setDisplayShowTitleEnabled (false); } // Hide the Toolbar throughsetToolbar reformulates the Toolbar public voidhidetoolBar(){ commonTitleTb.setVisibility(View.GONE); } // Menu click event public voidsetToolBarMenuOnClick(Toolbar.OnMenuItemClickListener onClick){ commonTitleTb.setOnMenuItemClickListener(onClick); } // Set the upper-left back button to public voidsetBackArrow(){ final Drawable upArrow = getResources().getDrawable(R.drawable.back); // Set the icon to the left of the Toolbar getSupportActionBar().sethomeasupIndicator (upArrow); getSupportActionBar().setDisplayHomeAsUpEnabled(true); / / set back button click event commonTitleTb setNavigationOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { finish(); }}); } // Set the icon in the upper right corner to public voidsetRightImagine(@DrawableRes int imgId){ img.setImageResource(imgId); } // Set the contents of the content area under the Toolbar public voidsetContentLayout(int layoutId){ LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View contentView = inflater.inflate(layoutId,null); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); content.addView(contentView,params); } // Set the title public voidsetTitle(String title){
        if(! TextUtils.isEmpty(title)){ commonTitleTv.setText(title); }} // Set the title public voidsetTitle(int resId){ commonTitleTv.setText(resId); }}Copy the code

The code in The BaseActivity can be changed to suit your needs, or you can change the BaseActivity to an abstract class so that the Activity can override abstract methods. The setContentLayout() method is important. It adds the Toolbar and everything else underneath it

activity_base.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="? actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/commom_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="I am the title."
            android:textSize="20sp"
            android:textColor="@color/white"/>

        <ImageView
            android:id="@+id/commom_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"/>

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</LinearLayout>
Copy the code

FrameLayout is used to put content in your Activity.

RecommendedDailyActivity:

public class RecommendedDailyActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Recommended of the Day");
        setBackArrow();
        setRightImagine(R.drawable.question); }}Copy the code

Recently I have seen a lot of code module packaging because I feel that my project is very jumble want to reduce repeatability behind will be in the practice of BaseFragment RecyclerView network framework and its page jump packaging come on!!