Android developers --Toolbar commonly used settings

Benpian note Toolbar for recording common settings such as color settings Toolbar, the return button display, three display dot to the right of the button

Android ActionBar before use, Android5.0 begin, the official recommended to use Google Toolbar instead ActionBar

Recently started slowly on kotlin, and posted the code may be kotlin code, forgive me, if there is Java-based, in fact, quite simple to use, you can refer to my study notes kotlin

Kotlin study notes

1. Alternatively ActionBar Toolbar

We first theme is set to NoActionBar, after adding ToolBar layout xml file

By the Android Manifest file into the Theme, modify Theme

<!-- 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>
</style>

Layout xml file, add the Toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:fitsSystemWindows="true"
    android:layout_height="match_parent"
    tools:context="com.wan.noveldownloader.activity.MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:titleTextColor="@color/white"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>

Thereafter, Activity code used setSupportToolbar, disposed inside the toolbar

setContentView(R.layout.activity_main);
//findviewbyid找到toolbar实例
setSupportToolbar(toolbar);

After running you can see the results

2. Modify the title text

The default text Toolbar shown is the APP project your current label, we modify the label to AndroidManifest file attributes Activity, you can achieve the effect of modifying the text

The figure above, I have two APP Activity, which, MainActivity the toolbar is not defined label properties, so the default label property equal to the name of the project, all of the display is the "Star fiction downloader"

While the other has a label that SettingActivity the property, all the text is displayed in the "Settings"

PS: If you do not want to display text, then by getSupportActionBar().setDisplayShowTitleEnabled(false)(after setSupportToolbar method) to achieve

3. Modify the color

Modify the background color

Modify the background color by modifying the toolbar to backgroundachieve the desired effect properties

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:background="@color/colorPrimary"
    android:layout_height="wrap_content"/>

Edit the title text color

Modify the titleTextColorattributes necessary to introduce app namespace

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    app:titleTextColor="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

4. Back button displayed left

Through the left return button display code

setSupportActionBar(toolbar)
getSupportActionBar().setHomeButtonEnabled(true)
getSupportActionBar().setDisplayHomeAsUpEnabled(true)

Activity also need to override the onOptionsItemSelectedmethods, click Back button to achieve the effect of return

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    if(item.itemId == android.R.id.home){
        finish()
    }
    return super.onOptionsItemSelected(item)
}

The display of the menu button Toolbar

1. 创建 menu.xml

Create a menu of the folder under the res directory, then create a new folder in the menumenu.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:title="设置" android:id="@+id/menu_setting" app:showAsAction="always" android:icon="@drawable/icon_setting"/>
</menu>
  • title title
  • icon icon
  • showAsAction
    this property has several choices
    1. always: This value will make the menu items remain on the Action Bar.
    2. ifRoom: If there is enough space, this value will make the menu items displayed on the Action Bar.
    3. never: The value of the menu item never appeared on the Action Bar.
    4. withText: The value of the menu item and its icon, menu text displayed together.

2. The method of rewriting onCreateMenu

Activity Method onCreateMenu rewriting of the file is loaded into the APP menu.xml

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu,menu)
    return true
}

3. The method of rewriting opOptionSelect

Set each menu click event, and set up similar operations listener

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    if (item?.itemId ==R.id.menu_setting) {
        startActivity(SettingActivity::class.java)
    }
    return false
}

4.setSupportToolbar

And the same as the previous steps

Guess you like

Origin www.cnblogs.com/kexing/p/11620853.html