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

后端 未结 7 1774
旧时难觅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:

    <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:titleTextColor="#ffffff"
                    android:background="#ECA539"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    

    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);
    }
    
    0 讨论(0)
提交回复
热议问题