ToolBar就是用来替换这个东西的
ToolBar常用属性
我们想用ToolBar的话先把原来的给禁用掉
<?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:layout_height="match_parent" tools:context=".MainActivity"> <!-- titleMarginStart title距左边的距离--> <androidx.appcompat.widget.Toolbar android:id="@+id/TB" android:background="#ff00ff" android:layout_width="match_parent" android:layout_height="?actionBarSize" app:navigationIcon="@drawable/ic_arrow_back_black_24dp" app:title="标题" app:titleTextColor="#FFFFFF" app:titleMarginStart="120dp" app:subtitle="小标题" app:subtitleTextColor="#FFFF00"/> </LinearLayout>
package com.example.senventoolbar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.TB); //点自己设置的navigationIcon会有事件出来 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"点击了ToolBar",Toast.LENGTH_SHORT).show(); } }); } }