Add Icon to the left of the title in the action bar in android

后端 未结 7 1770
旧时难觅i
旧时难觅i 2021-02-07 01:07

I want to add a clickable icon/button to the left of the title in the action bar . How to do it? Following is the code with which I have added search and setting icon to the act

7条回答
  •  情歌与酒
    2021-02-07 01:51

    One thing is that by default title and app icon set into the left and other than icons sent into the right side. If you want to add your icons into the left side there is a way to them into to left side which is:

    step 1. create a custom layout which having search, setting icon and your title as you want.

    step 2. create a toolBar layout like:

    
    

    step 3. inflate your custom layout into the toolbar like:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    initToolbar();
    
    }
    private void initToolBar() {
    setSupportActionBar(toolbar);
        View customlayout= getLayoutInflater().inflate(R.layout.custom_layout, null);
        toolbar.addView(logo);
    }
    

提交回复
热议问题