java - 如何将自定义按钮添加到工具栏
问题描述
我是 Android 开发新手,需要一些帮助 :) 我只是想在工具栏右侧添加自定义按钮。
我看到很少有 LinearLayouts 等解决方案......但我不确定这样做是否正确。
已经检查了 Material design 并找到了 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
}
});
推荐阅读
- internet-explorer-11 - AppSource 的任何替代方案以支持 IE11 来托管 Office 插件
- mysql - 重复记录 - 删除语句不起作用
- python - 将 jdk 与 python venv 集成
- database - IOException 发生在 Apache IoTDB 的合并任务中
- bash - 在 bash shell 中获取当前时间(以秒为单位)的问题
- python - 如何使用训练结果来预测测试数据并得到误分类错误?
- docker - 多阶段docker构建,防止特定阶段的并发
- reactjs - 将React函数式组件作为函数的参数传递时,我应该如何用TS声明函数参数类型
- c# - 如何解决“'default_aspx' 不包含 'Button1_Click' 的定义”错误?
- xamarin - Xamarin Forms 应用中的嵌入式 Power App