控件Toolbar

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

常用属性

android:layout_width=“match_parent” 宽度
android:layout_height="?attr/actionBarSize" 高度
android:background="#FFFF0000" 背景颜色
app:navigationIcon="@drawable/ic_baseline_arrow_back_24" 左上角的图标
app:title=“主标题” 主标题
app:titleTextColor="@color/white" 主标题颜色
app:titleMarginStart=“90dp” 间距
app:subtitle=“子标题” 子标题
app:subtitleTextColor="#ff00ff00" 子标题颜色
app:logo="@mipmap/ic_launcher" 设置安卓logo

注意:Toolbar的包是AndroidX的包
import androidx.appcompat.widget.Toolbar;

代码

MainActivity.java

package com.example.toolbar;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Toolbar第一种设置方式通过xml设置
        Toolbar toolbar = findViewById(R.id.tb_one);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Finny", "onClick: toolbar被点击了" );
            }
        });

        //Toolbar第二种设置方式通过java代码设置
        Toolbar toolbar2 = findViewById(R.id.tb_two);

        toolbar2.setTitle("主标题二");
        toolbar2.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
        toolbar2.setSubtitle("小标题2");
        toolbar2.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Finny", "onClick: toolbar2被点击了" );
            }
        });

        //设置标题居中,在Toolbar控件中加个textview控件
        Toolbar toolbar3 = findViewById(R.id.tb_three);
        toolbar3.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Finny", "onClick: toolbar3被点击了" );
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/tb_one"
        android:background="#FFFF0000"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
        app:title="主标题"
        app:titleTextColor="@color/white"
        app:titleMarginStart="90dp"
        app:subtitle="子标题"
        app:subtitleTextColor="#ff00ff00"
        app:logo="@mipmap/ic_launcher"/>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb_two"
        android:layout_marginTop="10dp"
        android:background="#FF0000FF"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb_three"
        android:background="#FF00FF00"
        android:layout_marginTop="20dp"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <TextView
            android:text="标题"
            android:textColor="@color/white"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </androidx.appcompat.widget.Toolbar>

</LinearLayout>

效果图

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小阿大:)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值