Android控件之 Toolbar(标题栏)的使用

在这里插入图片描述这个画红线的就是toolbar了。如果你正常创建一个项目,他是自带toolbar的,如果你想把系统的toolbar换成自己的toolbar,需要在:

在这里插入图片描述修改为NoActionBar!!!
从此刻开始,你就可以编写Toolbar了…

首先就是方法介绍:

		//Toolbar的标题
 	    android:background="#ffff00"
        app:title="主标题"
        app:titleTextColor="#ff0000"
        //主标题的起始位置
        app:titleMarginStart="90dp"
        app:subtitle="子标题"
        //子标题的文字颜色
        app:subtitleTextColor="#00ffff"
        app:logo="@drawable/ic_launcher_background"
        //导航按钮 一般是作为一个返回按钮使用
         app:navigationIcon="@drawable/ic_android_black_24dp"

接下来直接上代码 没什么好说的

package com.example.mytoolbar;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ToolbarActivity extends AppCompatActivity {

    //在这里注意使用的导包应该是androidx的导包 而不是android自带的
    /**
     *  androidx导包 import androidx.appcompat.widget.Toolbar;
     *  系统自带导包  import android.widget.Toolbar;
     */
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        toolbar = findViewById(R.id.tb);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ToolbarActivity.this, "返回", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
<?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=".ToolbarActivity">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tb"
        app:navigationIcon="@drawable/ic_android_black_24dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffff00"
        app:title="主标题"
        app:titleTextColor="#ff0000"
        app:titleMarginStart="90dp"
        app:subtitle="子标题"
        app:subtitleTextColor="#00ffff"
        app:logo="@drawable/ic_launcher_background"
        />



</LinearLayout>

上面只是第一种使用xml对toolbar进行设置,还有一种方式是使用代码设置toolbar,其实就是把xml的代码搬到页面里面去,我就不写了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值