常见属性详解
-
android:layout_width="match_parent"
-
android:layout_height="?attr/actionBarSize"
-
android:background="#ffff00"
-
app:navigationlcon="@drawable/ic_baseline_arrow_back_24"
-
app:title="主标题"
-
app:titleTextColor="#ff0000"
-
app:titleMarginStart="90dp"
-
app:subtitle="子标题"
-
app:subtitleTextColor="#00ffff"
-
app:logo="@mipmap/ic_launcher"
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_1"
android:background="#00ffff"
app:navigationIcon="@drawable/ic_baseline_keyboard_backspace_24"
app:logo="@drawable/icon_01"
app:titleTextColor="#FF00FF"
app:title="标题"
app:subtitleTextColor="#FF000000"
app:subtitle="子标题"
app:titleMarginStart="90dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_2"
android:background="#00ffff"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp"/>
<androidx.appcompat.widget.Toolbar
android:background="#00ffff"
app:navigationIcon="@drawable/ic_baseline_keyboard_backspace_24"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp">
<TextView
android:text="标题"
android:textSize="20sp"
android:layout_gravity="center"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.Toolbar>
设置监听事件
Toolbar tb_1 = findViewById(R.id.tb_1);
Toolbar tb_2 = findViewById(R.id.tb_2);
tb_1.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wu","tb_1被点击了");
}
});
tb_2.setNavigationIcon(R.drawable.ic_baseline_keyboard_backspace_24);
tb_2.setTitle("标题");
tb_2.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wu","tb_2被点击了");
}
});