zoukankan      html  css  js  c++  java
  • 001.android初级篇之ToolBar

    官方的最新support library v7中提供了新的组件ToolBar,用来替代之前的ActionBar,实现更为弹性的设计在 material design 也对之做了名称的定义:App bar。下面描述下它的基本用法,权作抛砖引玉。

    基本用法

    如下代码,实现了主副标题及Logo和标题颜色的设置

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("交易清单");
    toolbar.setSubtitle("--2015年度明细");
    toolbar.setLogo(R.drawable.icon);
    toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
    setSupportActionBar(toolbar);
    

    设置菜单按钮额操作

    需要首先编辑xml布局文件menu_main.xml

    <menu 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" tools:context=".MainActivity">
        <item android:id="@+id/action_settings" android:title="@string/action_settings"
            android:orderInCategory="100" app:showAsAction="never" />
        <item android:id="@+id/action_share" android:title="share"
            android:orderInCategory="100" app:showAsAction="ifRoom" />
    </menu>
    

    其中 app:showAsAction有三个可选的值

    always:总是显示在界面上
    never:不显示在界面上,只让出现在右边的三个点中
    ifRoom:如果有位置才显示,不然就出现在右边的三个点中
    

    android:orderInCategory

    表明摆放的顺序,不一定从0还是计算,但必须大于等于0,数值小的位于前,如果数值一样,在我们这个例子中3又两个值,则安顺序摆放;
    

    代码处理

    设置布局文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }
    设置键值处理

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            mTextView.setText("Your Press Setting!
    ");
            return true;
        }
        else if(id == R.id.action_share){
            mTextView.setText("share!");
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    去除ActionBar

    去除Actionbar最简单的方法就是直接继承NoActionBar的主题了

    参考链接

    1. android:ToolBar详解
    2. Android Material Design之Toolbar与Palette实践
  • 相关阅读:
    十天冲刺之一
    每日日报2021 4/30
    每日日报2021 4/29
    每日日报2021 4/28
    每日日报2021 4/27
    每日日报2021 4/26
    每日日报2021 4/24
    每日日报2021 4/23
    《梦断代码》读后感
    273. Integer to English Words
  • 原文地址:https://www.cnblogs.com/fly-fish/p/4902323.html
Copyright © 2011-2022 走看看