我阅读了有关ActionBar(App Bar)的documentation和this。它声明:
在最基本的表单中,操作栏显示的标题 一边是活动,另一边是溢出菜单 三点式弹出菜单)。
应用栏功能已逐渐添加到原生ActionBar中 各种Android版本。结果,本机ActionBar 根据Android系统的版本,行为会有所不同 设备可能正在使用。 相比之下,添加了最新功能 到支持库的工具栏版本。 因此,你 应该使用支持库的工具栏类来实现你的 活动' app bar 。
所以基本上它告诉我在我想自定义App Bar时使用工具栏而不是Actionbar。我听从了建议并添加了一个工具栏:
build.gradle(Module:app)文件:
dependencies {
...
compile 'com.android.support:appcompat-v7:25.2.0'
}
RES / styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
activity_main.xml中:
<RelativeLayout ... android:fitsSystemWindows="true" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<ImageView android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yourStory" android:src="@drawable/ic_face_black_24dp" />
</android.support.v7.widget.Toolbar>
操作栏似乎已被烘焙到工具栏中,因为即使我没有在工具栏中指定它,也会显示溢出菜单。此外,我的申请的标题仍然存在。正确显示工具栏中的图像。
现在,对我来说很重要的是,我确实对菜单项进行了充气,以便它们出现:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
在menu / main_menu.xml中:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/camera" android:title="Camera"
app:showAsAction="always"/>
<item android:id="@+id/logo" android:title="Logo"
app:showAsAction="always"/>
<item android:id="@+id/profile" android:title="Profile"
app:showAsAction="always"/>
</menu>
如果我摆脱这些项目,弹出菜单会消失。显然,ActionBar已集成到工具栏中。但是如何摆脱工具栏上的标题?我不知道它来自哪里。我的最终目标是创建类似于Instagram的工具栏。在左侧,它有一个摄像头图标,中间有一个徽标,右边是另一个图标。
答案 0 :(得分:0)
好的,我已经找到了一些明确的问题。有一行代码将ActionBar集成到ToolBar中,它是setSuppertActionBar
。一旦发生了这种集成,你就可以开始在工具栏上调用ActionBar方法:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Sets the Toolbar to act as the ActionBar for this Activity window.
setSupportActionBar(toolbar);
// We do not want to display title, which is a default for the ActionBar
getSupportActionBar().setDisplayShowTitleEnabled(false);
答案 1 :(得分:0)
您可以自定义Toolbar
,如下所示:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#fff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/camera"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:padding="8dp"
android:clickable="true"
android:src="@drawable/ic_camera"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/camera"
android:layout_toLeftOf="@id/send"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:src="@drawable/Instagram_logo"/>
<ImageView
android:id="@+id/send"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:padding="8dp"
android:clickable="true"
android:src="@drawable/ic_send"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
在activity
课程中写下以下代码行,隐藏默认工具栏title
并处理click
和camera
选项的send
事件:强>
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
ImageView imageCamera = (ImageView) toolBar.findViewById(R.id.camera);
ImageView imageSend = (ImageView) toolBar.findViewById(R.id.send);
imageCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
imageSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
<强>输出:强>