如何将自定义按钮添加到工具栏
How to add custom buttons to the ToolBar
我是 Android 开发的新手,需要一些帮助 :)
我只是想将自定义按钮添加到工具栏右侧。
我看到很少有 LinearLayouts 等解决方案...但我不确定这样做是否正确。
已经检查了 Material 设计并找到了 TopBar,但我认为这不是我需要的 :p
示例:
谢谢!
创建自定义工具栏(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
}
});
我是 Android 开发的新手,需要一些帮助 :) 我只是想将自定义按钮添加到工具栏右侧。
我看到很少有 LinearLayouts 等解决方案...但我不确定这样做是否正确。
已经检查了 Material 设计并找到了 TopBar,但我认为这不是我需要的 :p
示例:
谢谢!
创建自定义工具栏(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
}
});