如何给Android的ActionBar添加点击事件?
伊谢尔伦
伊谢尔伦 2017-04-17 12:02:06
[Android讨论组]

我的Activity有一个列表,想点击ActionBar后列表滚到最顶部,想问下如何给ActionBar添加点击事件。不用自定义的ActionBar,就用原生的。我试过给ActionBar添加一个CustomView,给Customview添加点击事件,是可行的。但是Customview要不太小无法撑慢整个ActionBar的高度,如果设置太大则会覆盖掉左上角的标题,求解。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
天蓬老师

如你所说,用CustomView

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="#FFF"
        android:text="我才是标题" />

</LinearLayout>

部分代码:

View customView = LayoutInflater.from(this).inflate(R.layout.back, new LinearLayout(this), false);
        getActionBar().setDisplayShowCustomEnabled(true);
        getActionBar().setCustomView(customView);
        customView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {   
                Toast.makeText(MainActivity.this, "....", 10000).show();
            }
        });

我自己测试过,暂无出现你所说的问题。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号