Android 学习笔记四:创建工具栏按钮

前面我们已经可以在一个Activity中添加一些按钮之类的组件。由于手机的屏幕很小,所以很多时候我们会需要用到工具栏,通过下拉菜单之类的方式来节省空间。

Android 提供了对工具栏按钮的强大支持。

增加一个工具栏按钮

我们现在给 MainActivity 增加一个搜索按钮。增加一个按钮需要做这三件事

一,在 res/menu/activity_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_search"
        android:icon="@drawable/ic_search_white_24dp"
        android:title="@string/action_search"
        android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="never" />
</menu>

注意其中我们用到了 drawablestring 中的两个资源,需要自己去创建一下。

二,在 MainActivity 中引用配置好的按钮

MainActivity 中已经声明了一个 onCreateOptionsMenu 方法了,其实根本不用改:

@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;
}

三,给按钮绑定事件

这里我们也已经有一个 onOptionsItemSelected 方法了,只需要在其中增加一下事件监听就行了

@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
    switch (id) {
        case R.id.action_search: Toast.makeText(this, "你点击了搜索", Toast.LENGTH_SHORT).show();
        case R.id.action_settings: Toast.makeText(this, "你点击了设置", Toast.LENGTH_SHORT).show();
    }

    return super.onOptionsItemSelected(item);
}

增加返回按钮

很多时候,我们需要给不是 MainActivity 的活动都增加一个返回按钮,以方便用户通过返回按钮能返回到上一个活动。安卓提供了返回按钮的默认支持,我们只需要配置一下即可,并不用写返回的逻辑代码。

只需要两步既可以实现返回按钮的功能。这里我们希望能在 ProfileActivity 中增加一个 返回按钮,点击返回 MainActivity

一,在 AndroidManifest.xml 中 Activity 的声明中加上父活动的声明:

  <activity
        android:name=".ProfileActivity"
        android:label="@string/title_activity_profile"
        android:parentActivityName=".MainActivity">
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />

        <intent-filter>
            <action android:name="com.lihongxun.Profile" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

注意,如果想支持 4.0 及以下设备需要加上 meta-data 那一行,不然就只需要加上 android:parentActivityName=".MainActivity” 即可

二,在 ProfileActivity 中启用返回按钮。在 onCreate 函数中加上一行代码即可:

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

然后再运行试试。

三 覆盖式工具栏

默认情况下,顶部的工具栏是占用空间的,他会把内容区挤到下面去。有时候你可能会希望它是覆盖在内容区上的,特别是你希望能动态显示隐藏工具栏的时候。
只需要在 styles.xml 中声明使用覆盖式工具栏即可

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowActionBarOverlay">true</item>
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>

中间的两行 item 就是声明覆盖式工具栏,注意第二行是兼容库的时候用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,我们可以使用 `TranslateAnimation` 类来实现 View 的平移动画效果。以下是从左向右平移的示例代码: ```java TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0); animation.setDuration(1000); // 动画持续时间,单位为毫秒 animation.setFillAfter(true); // 动画结束后保持状态 view.startAnimation(animation); // 开始动画 ``` 上述代码中,`TranslateAnimation` 的两个参数分别表示 View 在 X 轴和 Y 轴上的起点和终点位置。这里我们将 X 轴的起点设置为 0,终点设置为 200,表示 View 从左向右平移 200 个像素。 如果想要实现循环播放的效果,可以使用 `AnimationSet` 类来组合多个动画,示例代码如下: ```java TranslateAnimation animation1 = new TranslateAnimation(0, 200, 0, 0); animation1.setDuration(1000); animation1.setInterpolator(new LinearInterpolator()); // 线性插值器,使动画匀速播放 TranslateAnimation animation2 = new TranslateAnimation(200, 0, 0, 0); animation2.setDuration(1000); animation2.setStartOffset(1000); // 设置动画延迟开始的时间,单位为毫秒 animation2.setInterpolator(new LinearInterpolator()); AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(animation1); animationSet.addAnimation(animation2); animationSet.setRepeatCount(Animation.INFINITE); // 设置动画重复播放的次数 view.startAnimation(animationSet); ``` 上述代码中,我们创建了两个 `TranslateAnimation`,分别表示 View 从左向右平移和从右向左平移。然后使用 `AnimationSet` 将两个动画组合在一起,并设置为循环播放,效果如下: ![translate_animation.gif](https://img-blog.csdn.net/20180420202704371?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFuZ3VhZ2VfYmFpZHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75|watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFuZ3VhZ2VfYmFpZHU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75) 如果想要实现手势控制平移的效果,可以使用 `ViewPropertyAnimator` 类来实现,示例代码如下: ```java view.animate().translationX(200); // 平移 View 到 X 轴 200 像素的位置 ``` 上述代码中,我们使用 `animate()` 方法获取 `ViewPropertyAnimator` 对象,然后调用 `translationX()` 方法实现 View 的平移动画。注意,这种方式只能实现单次平移动画,不能循环播放。 除了平移动画,Android 中还有旋转和缩放动画,可以使用 `RotateAnimation` 和 `ScaleAnimation` 类来实现。具体用法与 `TranslateAnimation` 类似。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值