天天看点

Android ToolBar的使用

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/64130211

本文出自【赵彦军的博客】

前言

ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话,可以添加support-v7包.

要使用ToolBar,首先引入v7包

还需要在使用 ToolBar 的 Activity 下面添加去掉 ActionBar 的主题 :

比如:

<activity
     android:name=".MainActivity"
     android:theme="@style/Theme.AppCompat.Light.NoActionBar"
       >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
           

如果没有添加去掉 ActionBar 的主题 , 会报异常:

Android ToolBar的使用

ToolBar 的使用

创建

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    >

   <!-- android:layout_height="?attr/actionBarSize" 根据手机分辨率自动分配高度-->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorAccent"
        android:layout_height="?attr/actionBarSize"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="个人中心"
                android:layout_centerInParent="true"
                android:textColor="#ffffff"
                android:textSize="20sp"
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:layout_alignParentRight="true"
                />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>


</RelativeLayout>
           

MainActivity 的代码逻辑

package com.constraintlayout.app;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        mToolbar.setTitleTextColor(Color.YELLOW);

        //取代原本的actionbar
        setSupportActionBar(mToolbar);

        //ToolBar里面还可以包含子控件
        mToolbar.findViewById(R.id.tv1 ).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击自定义按钮", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
           

效果图:

Android ToolBar的使用

继续阅读