- 设置导航图标并监听事件
package com.wust.selftoolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar tb_self_tool_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_self_tool_bar = findViewById(R.id.tb_self_tool_bar);
//设置 Toolbar
setSupportActionBar(tb_self_tool_bar);
//设置 左边导航
tb_self_tool_bar.setNavigationIcon(R.drawable.ic_launcher_foreground);
tb_self_tool_bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,“导航按钮”,Toast.LENGTH_SHORT).show();
}
});
}
}
- 设置logon
package com.wust.selftoolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar tb_self_tool_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_self_tool_bar = findViewById(R.id.tb_self_tool_bar);
//设置 Toolbar
setSupportActionBar(tb_self_tool_bar);
//设置 左边导航
tb_self_tool_bar.setNavigationIcon(R.drawable.ic_launcher_foreground);
tb_self_tool_bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,“导航按钮”,Toast.LENGTH_SHORT).show();
}
});
//设置 logon
tb_self_tool_bar.setLogo(R.mipmap.ic_launcher_round);
}
}
- 设置父子级标题
package com.wust.selftoolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar tb_self_tool_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_self_tool_bar = findViewById(R.id.tb_self_tool_bar);
//设置 Toolbar
setSupportActionBar(tb_self_tool_bar);
//设置 左边导航
tb_self_tool_bar.setNavigationIcon(R.drawable.ic_launcher_foreground);
tb_self_tool_bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,“导航按钮”,Toast.LENGTH_SHORT).show();
}
});
//设置 logon
tb_self_tool_bar.setLogo(R.mipmap.ic_launcher_round);
//设置 父子级标题 注意:这里不能使用 tb_self_tool_bar.setTitle(“我是一级标题”); 要不然还是应用的名字
getSupportActionBar().setTitle(“我是一级标题”);
tb_self_tool_bar.setSubtitle(“我是二级标题”);
}
}
- 设置 Menu
package com.wust.selftoolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar tb_self_tool_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_self_tool_bar = findViewById(R.id.tb_self_tool_bar);
//设置 Toolbar
setSupportActionBar(tb_self_tool_bar);
}
//设置Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.self_menu,menu);
return super.onCreateOptionsMenu(menu);
}
}
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<item
android:id=“@+id/menu_settings”
android:orderInCategory=“1”
android:title=“one”
app:showAsAction=“never” />
<item
android:id=“@+id/menu_about”
android:orderInCategory=“2”
android:title=“two”
app:showAsAction=“ifRoom”/>
<item
android:id=“@+id/menu_quit”
android:orderInCategory=“3”
android:title=“three”
app:showAsAction=“always”/>
- 设置 Menu 的样式
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:orientation=“vertical”
tools:context=“.MainActivity”>
<androidx.appcompat.widget.Toolbar
android:id=“@+id/tb_self_tool_bar”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“#0000ff”
android:titleTextColor=“#ffffff”
app:popupTheme=“@style/popupStyle”/>
最后
愿你有一天,真爱自己,善待自己。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!