第一步就是将我们的主要的布局文件编写出来:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--接下来的话就是我们的toobar做actionbar用-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toobar"
android:background="#00FFE7"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"/>
</LinearLayout>
然后的话就是将我们的theme文件中的就是
<style name="Theme.Toobarall" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
改为
<style name="Theme.Toobarall" parent="Theme.MaterialComponents.DayNight.NoActionBar">
修改后的样子:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Toobarall" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
然后的话就是添加我们的菜单项:
位置:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.lhtb.toolbar.MainActivity">
<!--在我们的这个xml的布局文件中设置我们的菜单项-->
<item
app:actionViewClass="android.widget.SearchView"
android:title="搜索"
android:id="@+id/search"
android:orderInCategory="80"
app:showAsAction="never"
/>
<item
android:icon="@drawable/abc_vector_test"
android:title="分享"
android:id="@+id/share"
android:orderInCategory="90"
app:showAsAction="never"
/>
<item
android:icon="@drawable/ic_baseline_apps_24"
android:title="设置"
android:id="@+id/setting"
android:orderInCategory="80"
app:showAsAction="never"
/>
</menu>
主要的java代码:
package com.example.toobarall;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// todo 1 在我们的java中的代码
toolbar = (Toolbar) findViewById(R.id.toobar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 设置我们的toobar支持我们的actionbar
setSupportActionBar(toolbar);
// todo 设置我们的logo
toolbar.setLogo(R.drawable.ic_baseline_apps_24);
// todo 设置我们的导航菜单
toolbar.setNavigationIcon(R.drawable.ic_launcher_foreground);
// todo 设置我们的主标题
toolbar.setTitle("智慧城市");
// todo 设置我们的副标题
toolbar.setSubtitle("赋能未来");
//设置我们的弹出菜单的颜色和背景
toolbar.setPopupTheme(R.style.Widget_AppCompat_ActionBar_Solid);
// 设置我们的菜单
toolbar.inflateMenu(R.menu.main);
// todo 设置我们的点击事件
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.search:
Toast.makeText(MainActivity.this,"搜索",Toast.LENGTH_LONG).show();
break;
case R.id.share:
Toast.makeText(MainActivity.this, "分享",Toast.LENGTH_LONG).show();
break;
case R.id.setting:
Toast.makeText(MainActivity.this,"设置", Toast.LENGTH_LONG).show();
break;
default:
}
return true;
}
});
}
// todo 复写onCreateOptionsMenu实现点加的回调
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
}