Android权威编程指南笔记 第十三章 工具栏(toolBar)

第十三章 工具栏(toolBar)

  • 步骤:
  1. 定义菜单布局文件。
    New -> Android Source file -> Menu选项
  2. 确定布局文件中的图标,有以下几种方法:
    1.创建定制图标,放到不同的资源密度文件中。
    2.找到合适的系统图标,但由于各设备系统图标可能不同,会破坏整体设计风格。不推荐。
    (打开SDK目录,platforms/ android-*/ data/ res)
    3.使用Android Studio内置的Android Asset Studio工具(New -> Image Asset)
  3. 实现菜单的回调函数
    1.onCreateOptionsMenu(Menu, MenuInflater)
    2.onOptionsItemSelected(Menu)
  4. 可选择实现层级式导航

菜单布局文件

<?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/new_crime"
        android:icon="@drawable/ic_menu_add"
        android:title="@string/new_crime"
        app:showAsAction="ifRoom|withText"/>

    <item
        android:id="@+id/show_subtitle"
        android:title="@string/show_subtitle"
        app:showAsAction="ifRoom"
        />

</menu>
  • 属性showAsAction有always,ifRoom, never三个属性值可供选择。
  • android:showAsAction属性是随操作栏的发布才添加(即原生老版本不可用),为了兼容性考虑,使用了app的命名空间。

实现菜单栏的回调函数

  • onCreateOptionsMenu方法是fm调用的,应在其管理的fragment中通知fm。即在fragment中添加setHasOptionsMenu(true);
  • onOptionsItemSelected方法返回的时boolean属性的值,一旦完成处理事件,应返回true。用switchj及case来对菜单资源Id进行判断。
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime, menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.delet_crime:
                CrimeLab.get(getActivity()).deletCrime(mCrime);
                getActivity().finish();
                return true;

            default:
                    return super.onOptionsItemSelected(item);
        }
    }

实现层级式导航

        <activity
            android:name=".CrimePagerActivity"
            android:parentActivityName=".CrimeListActivity"> <!--加上父activity-->
        </activity>
  • 解决层级式导航父activity会被重建的问题
  1. 覆盖向上导航机制调用activity的finish()方法直接回退到前一个activity界面,但只能回退一个层级。
  2. 在当前activity中覆盖getParentActivityIntent(),用附带的extra信息重建。但这样就需要知道父类的情况,不利于独立性。

相关小记

  • 菜单文件名遵循与布局文件名一样的原则,即反向,小写,加下划线。
  • 设置菜单栏标题
        AppCompatActivity activity = (AppCompatActivity) getActivity();//通过强制类型转换以达到访问工具栏的效果
        activity.getSupportActionBar().setSubtitle(subtitle);
  • 菜单项发生改变,需要更新时getActivity().invalidateOptionsMenu();
  • 工具栏可以在屏幕上的任何位置摆放,也可以摆放多个,还能内嵌视图和调整高度。
  • <string name="subtitle_format">%1$d crimes</string>
    String subtitle = getString(R.string.subtitle_format, crimeCount); //该方法接受字符串,与占位符的值替换

挑战练习

  • 删除Crime记录
    跟第十四章的挑战练习类似
  • 复数字符串资源
    按照示例代码就可以,但由于首选语言为中文, ,不会有单复数的变化. 选为英文就可以了.
  • 当没有数据时设置空视图。
  1. 添加一个TextView和Button
  2. 实例化他们, 在Button上设置点击监听
  3. updateUI中进行判断
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/crime_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv_show_tip">

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

    <TextView
        android:id="@+id/tv_show_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/tv_show_tip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/crime_recycler_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bt_new_crime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bt_new_crime"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_show_tip" />

</android.support.constraint.ConstraintLayout>
        if (crimes.size() != 0){
            mTextView.setVisibility(View.INVISIBLE);
            mButton.setVisibility(View.INVISIBLE);
        }else {
            mTextView.setVisibility(View.VISIBLE);
            mButton.setVisibility(View.VISIBLE);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值