【Android学习】ToolBar使用

Toolbar使用,Android studio 3.5.0,Android手机 华为Mate10pro ,Android9.0

MainActiviey代码

package com.example.toolbar;

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

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.item1:
            {
                Toast.makeText(MainActivity.this,"Item1 Clicked",Toast.LENGTH_LONG).show();
                break;
            }
            case R.id.item2:
            {
                Toast.makeText(MainActivity.this,"Item2 Clicked",Toast.LENGTH_LONG).show();
                break;
            }
            case R.id.item3:
            {
                Toast.makeText(MainActivity.this,"Item3 Clicked",Toast.LENGTH_LONG).show();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }
}

main_item.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/item1"
        android:title="item1"
        android:icon="@drawable/ic_launcher_foreground"
        app:showAsAction="never" ></item>
    <item android:icon="@drawable/ic_launcher_foreground"
        android:title="item2"
        android:id="@+id/item2"
        app:showAsAction="never" ></item>
    <item android:icon="@drawable/ic_launcher_foreground"
        android:title="item3"
        android:id="@+id/item3"
        app:showAsAction="never" ></item>
</menu>

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="overlapAnchor">false</item>
    </style>
</resources>

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.appcompat.widget.Toolbar
            app:title="ToolBar"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#25f"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" ></androidx.appcompat.widget.Toolbar>
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

最终效果如下,基本和ActionBar一样

【Android学习】ToolBar使用

 

上一篇:Smobiler APP开发----ToolBar和TabPageView组合使用


下一篇:django-debug-toolbar