【问题标题】:How to add custom buttons to the ToolBar如何将自定义按钮添加到工具栏
【发布时间】:2019-02-06 02:31:03
【问题描述】:

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

我看到很少有使用 LinearLayouts 等的解决方案......但我不确定这样做是否正确。

已经检查了 Material design 并找到了 TopBar 但不认为这是我需要的:p

示例:

谢谢!

【问题讨论】:

  • I saw few solutions with LinearLayouts and etc... But i'm not sure is it right way to do this. 是的,您可以使用它来实现自定义设计
  • 什么自定义图标?对于左边的Follow this,右边的图标可以是一个菜单,所以你只需要学习如何添加菜单。

标签: java android toolbar


【解决方案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  
            }
        });

【讨论】:

  • 不起作用,这样做布局会覆盖标题
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多