Toolbar 在 Android 中的使用,2021 大厂面试合集
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);
setActionBar(tb_self_tool_bar);
}
}
效果展示
高级使用
====
重要提示:经过我自己的测试,我觉得想进行高级设置最好使用下面这个包
androidx.appcompat.widget.Toolbar 对应 setSupportActionBar()
设置导航图标并监听事件
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);
评论