android:toolbar的简单应用

一,代码:

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.AnimActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="#008B00"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#ffff00"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        tools:ignore="MissingConstraints">

java:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_anim);

        //toolbar的属性
        Toolbar tl_head = (Toolbar) findViewById(R.id.toolbar);
        tl_head.setBackgroundColor(0xFF0000FF);
        tl_head.setLogo(R.drawable.a);
        tl_head.setTitle("标题");
        tl_head.setSubtitle("副标题");
        tl_head.setNavigationIcon(R.drawable.icon_back_white);
        setSupportActionBar(tl_head);
        //导航按钮的被点击
        tl_head.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

 

二,测试效果:

 

posted @ 2025-05-24 17:55  刘宏缔的架构森林  阅读(7)  评论(0)    收藏  举报