简体   繁体   中英

How to add custom buttons to the ToolBar

I'm new to Android development and need some help :) I just wanted to add custom buttons to toolbar right.

I saw few solutions with LinearLayouts and etc... But i'm not sure is it right way to do this.

Already checked Material design and found TopBar but don't think this is what i need :p

Example: 在此处输入图片说明

Thanks!

Create custom toolbar (toolbar.xml), you can use LinearLayout to house your custom design:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="100dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right">

                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    </android.support.v7.widget.Toolbar>

Include custom toolbar into your activity.xml:

<include
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   layout="@layout/toolbar" />

To access Button :

Button button = (Button) findViewById(R.id.button);


button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Your logic  
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM