如何自定义按钮添加到工具栏

问题描述 投票:0回答:1

我是新来的Android的开发,需要一些帮助:)我只是想添加自定义按钮的工具栏右侧。

我看到LinearLayouts和等几个解决方案...但我不知道是不是这样做正确的方式。

已经检查材料的设计和发现的顶栏,但不认为这是我需要的:P

例如:enter image description here

谢谢!

java android toolbar
1个回答
1
投票

创建自定义工具栏(toolbar.xml),你可以使用LinearLayout容纳您的自定义设计:

<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>

包括自定义工具栏到您的activity.xml:

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

要访问Button

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


button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Your logic  
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.