android 在同一个活动中添加两个工具栏?

2023-11-21

我有一个底部菜单作为工具栏,我需要在顶部添加另一个菜单作为操作栏。这是活动的代码

public class ListViewPharms extends AppCompatActivity {
public Toolbar mToolbar;
ListView mListView;
SimpleAdapter pharmAdapter;
LinearLayout searchsec;
// Search EditText
EditText inputSearch;


@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_action_bar);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_list_view_pharms_title);



    Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    setupEvenlyDistributedToolbar();



    inputSearch = (EditText) findViewById(R.id.search_bar);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ListViewPharms.this.pharmAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.menu_register, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_home:
            Intent intent = new Intent(this, ListViewPharms.class);
            startActivity(intent);
            break;
        case R.id.action_cont:
            item.setIcon(getResources().getDrawable(R.drawable.icon2_active));

            Intent intent2 = new Intent(this, Contribute.class);
            startActivity(intent2);
            break;
        case R.id.action_info:
            Intent intent3 = new Intent(this, info.class);
            startActivity(intent3);
            break;
        case R.id.action_settings:
            Intent intent4 = new Intent(this, contactInfo.class);
            startActivity(intent4);
            break;
    }
    return true;
}


/**
 * This method will take however many items you have in your
 * menu/menu_main.xml and distribute them across your devices screen
 * evenly using a Toolbar. Enjoy!!
 */
public void setupEvenlyDistributedToolbar(){
    // Use Display metrics to get Screen Dimensions
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    // Toolbar
    mToolbar = (Toolbar) findViewById(R.id.mytoolbar);
    // Inflate your menu
    mToolbar.inflateMenu(R.menu.menu_register);

    // Add 10 spacing on either side of the toolbar
    mToolbar.setContentInsetsAbsolute(4, 4);

    // Get the ChildCount of your Toolbar, this should only be 1
    int childCount = mToolbar.getChildCount();
    // Get the Screen Width in pixels
    int screenWidth = metrics.widthPixels;

    // Create the Toolbar Params based on the screenWidth
    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, ViewGroup.LayoutParams.WRAP_CONTENT);

    // Loop through the child Items
    for(int i = 0; i < childCount; i++){
        // Get the item at the current index
        View childView = mToolbar.getChildAt(i);
        // If its a ViewGroup
        if(childView instanceof ViewGroup){
            // Set its layout params
            childView.setLayoutParams(toolbarParams);
            // Get the child count of this view group, and compute the item widths based on this count & screen size
            int innerChildCount = ((ViewGroup) childView).getChildCount();
            int itemWidth  = (screenWidth / innerChildCount);
            // Create layout params for the ActionMenuView
            ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
            // Loop through the children
            for(int j = 0; j < innerChildCount; j++){
                View grandChild = ((ViewGroup) childView).getChildAt(j);
                if(grandChild instanceof ActionMenuItemView){
                    // set the layout parameters on each View
                    grandChild.setLayoutParams(params);
                }
            }
            }
        }
    }
}

这是此活动的 xml mytoolbar 是底部工具栏:

<RelativeLayout
    android:id="@+id/pharms_list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_above="@+id/mytoolbar">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"

        android:drawSelectorOnTop="false"
        android:layout_below="@+id/first_sec"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

<android.support.v7.widget.Toolbar
    android:id="@+id/mytoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/linesup"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"
    >
</android.support.v7.widget.Toolbar>

在清单文件中,我添加了 android:uiOptions="splitActionBarWhenNarrow" 如何将菜单添加到顶部作为此图像?

enter image description here


我有类似的需求,唯一的区别是我想要顶部有一个工具栏,底部有一个操作栏。

工具栏必须符合材料要求,有标题、一些按钮,可能还包括一个动作溢出,而底部栏必须包含一些等距、始终可见的按钮,没有别的。

我的实现如下,在 Android 4.x 和 Android 5.x 上经过测试和运行。

这是布局;正如您所看到的,我没有使用 Toolbar 作为底部栏,而是使用 ActionMenuView,它是一个较低级别的小部件,工具栏本身在内部使用:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/page_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"/>

    <android.support.v7.widget.ActionMenuView
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp" />

    <WebView
        android:id="@+id/page"
        android:layout_below="@id/page_toolbar"
        android:layout_above="@id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

这是底部栏的菜单;与常规工具栏菜单相比,没有什么不同:

<?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/action_list"
        android:icon="@drawable/ic_list"
        android:title="@string/action_list"
        app:showAsAction="always"/>

    <!-- several more items here... -->

</menu>

这是我的活动中的代码,它派生自 AppCompatActivity;如您所见,我利用顶部工具栏的 onCreateOptionsMenu 回调来膨胀并初始化底部栏。不幸的是,底部栏菜单项的侦听器需要单独设置。但它们可以与顶部工具栏共享相同的 onOptionsItemSelected:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the toolbar menu
    getMenuInflater().inflate(R.menu.web_page_menu, menu);

    // Inflate and initialize the bottom menu
    ActionMenuView bottomBar = (ActionMenuView)findViewById(R.id.bottom_toolbar);
    Menu bottomMenu = bottomBar.getMenu();
    getMenuInflater().inflate(R.menu.obj_menu, bottomMenu);
    for (int i = 0; i < bottomMenu.size(); i++) {
        bottomMenu.getItem(i).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return onOptionsItemSelected(item);
            }
        });
    }

    return true;
}

截图:

enter image description here

我不确定这是否是一个完全正统的实现,但我希望它对某人有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 在同一个活动中添加两个工具栏? 的相关文章

  • 如何用Android做交互动画(翻译)

    我在 Android 中有一些 png 序列 我需要将它们的 x 和 y 位置从屏幕顶部到底部的翻译动画化 当动画发生时 我需要对象来接收单击事件 我知道这在 3 0 之前的 Android 版本中效果不太好 因为display对象的位置与
  • 即使我单击“运行”,Eclipse 也会运行调试模式

    Eclipse 总是在调试模式下启动我的应用程序 即使我单击常规的 运行 按钮 有任何想法吗 我发现我必须重新启动 Xoom 才能使其再次正常工作
  • 使用 ColorMatrix 调整亮度

    我正在尝试使用 ColorMatrix 调整图像的亮度 当尝试调整色相时 您可以在 Photoshop 中看到此选项 亮度和亮度也是两个不同的功能 但我不知道要更改哪些值才能实现此目的 目前我可以使用此代码更改色调 public stati
  • Kotlin Android Firebase 数据库哈希映射转换为类

    我正在尝试从 firebase 数据库获取数据 断点显示它正在获取数据 但看起来我没有正确地将其分配给我的班级 这会导致此异常 java lang ClassCastException 无法将 java util HashMap 转换为 班
  • 清晰的图标 清晰的 Android 应用程序

    你好 下午好 关于如何提高图标的 png 质量 有什么想法吗 我使用了 Android 开发者页面上的套件 但我无法获得清晰的图像 我的意思是非常清晰 我是否需要以不同的方式加载此图标 而不仅仅是拖入我的布局 谢谢 我使用这个布局
  • android:ScrollView(或视差)内的RecyclerView

    我有一个片段2 次卡片浏览内有多个控件 below我有第二个卡片视图回收者视图 这有效perfect 问题是 recyclerview 启动了屏幕的最底部 并且滚动recyclerview非常small 以前使用过列表视图 这使我可以适应您
  • 如何使用retrofit2动态设置超时?

    public class Router private static Retrofit retrofit null public Retrofit getRetrofit if retrofit null OkHttpClient clie
  • 在 Android 市场中以编程方式检查我的应用程序版本

    目前 我正在启动时检查应用程序版本代码 并将其与我的服务器上的最新版本代码进行匹配 并根据此匹配 我发送用户从 Android 市场获取最新更新 它运行良好 但我的问题是我必须手动更改服务器上的最新版本代码 并且我不知道新版本何时发布APK
  • 如何连接到Google Play服务并加载排行榜

    我想将我的游戏与 Google Play 服务连接 我已阅读有关 Android 开发人员的文档 并尝试遵循输入数字示例 但仍然无法加载排行榜 我有导入baseGameUtils 但我使用andengine 所以我没有使用来自谷歌的exte
  • 使用 RoboSpice 有没有办法从异常中获取 HTTP 错误代码?

    我正在编写一个使用 RoboSpice 的应用程序 在请求侦听器 onRequestFailure SpiceException arg0 中 有没有办法确定该错误是由于发生 401 HTTP 错误而导致的 我有一个后端服务 当令牌过期时
  • 构建应用程序时出现 BufferOverflowException

    每次我想运行我的 Android 应用程序时 我都会收到错误 2013 11 02 13 05 36 Dex Loader Unable to execute dex java nio BufferOverflowException Che
  • Android 游戏偶尔出现延迟

    我正在用 Java 制作一个简单的 Android 游戏 我注意到每 20 40 秒就会出现一些烦人的延迟 首先 我认为它们是由垃圾收集器引起的 但当我检查 LogCat 时 我发现游戏滞后时没有垃圾收集 每当游戏开始滞后时 我都会标记日志
  • 在 Android 中使用 AES 加密的最佳实践是什么?

    我为什么问这个问题 我知道人们对 AES 加密存在很多疑问 即使对于 Android 也是如此 如果您在网络上搜索 会发现很多代码片段 但在每个页面上 在每个 Stack Overflow 问题中 我都发现了另一个具有重大差异的实现 所以我
  • foo.setVisibility(View.GONE) 和parent.removeView(foo) 之间的区别

    如果 foo 是一个视图 那么有什么区别foo setVisibility View GONE and fooParent removeView foo 我对两个语句之前和之后视图的内存消耗特别感兴趣 可见性设置为 GONE 的视图是否会消
  • android - 如何让按钮每次按下时单击播放声音文件?

    我打开了一个新项目 现在我想做的是 通过按下按钮 我想要播放一个 mp3 文件 而且每次按下按钮时 声音文件都会再次从头开始播放 所以假设 mp3 长 10 秒 我按下按钮 它正在播放 4 秒后我再次按下按钮 声音将再次播放 现在我想知道的
  • JavaScript 点击事件在 Android 4.0.4 默认浏览器上的 Samsung Galaxy SIII 上不起作用

    我在 Android 4 0 4 上使用 Samsung Galaxy SIII 的默认浏览器时遇到了最奇怪的问题 对于以下页面 单击链接将不会触发 JavaScript 处理程序 从其中一个 div 的内容中删除单个 a 字母使它们再次工
  • Android SDK WebView调用Activity

    我试图在单击 WebView 组件内的链接时启动活动 我的Webview已加载到里面Main java我想启动SubActivity java当点击网站内的链接时Main java 另外 如何将参数传递给此活动 Example inspec
  • 将主题应用到 v7 支持操作栏

    我正在使用support v7库来实现ActionBar在我的应用程序中 我的styles xml file
  • Android Webview隐私浏览

    我在我的 Android 应用程序中使用 webview 从多个站点获取一些网页 我对 webview 行为有一些疑问 webview 是否存储历史记录 cookie 表单自动填充信息 如果是的话 我们可以阻止它这样做吗 如果 Webvie
  • android.view.WindowLeaked - 使用对话框和新意图时

    我已经尝试了 stackoverflow 上提供的所有可能的解决方案 但我仍然在 logcat 中遇到此错误 活动 com xyz MainActivity 泄露了最初在此处添加的窗口 com android internal policy

随机推荐

  • 空间局部性与时间局部性

    我理解这些术语的定义 但我无法将它们的概念应用到代码中 在练习中 我们需要描述以下代码是空间代码还是时间代码 for int i 0 i lt 10 i printf some array i 我觉得这是空间局部性 因为当访问数组的一个索引
  • Spring Boot 2 禁用具有多个数据源的自动提交 Hikari

    我正在尝试关闭 Hikari 上具有多个数据源的自动提交 但我没有任何运气 我正在使用 Spring Boot 2 2 0 3 RELEASE 这是我的配置 应用程序属性 spring datasource primary driver c
  • 如何在 Laravel 中过滤多对多结构

    我有多对多的用户和角色结构 users id name roles id name 角色 用户 user id role id Model User php public function roles return this gt belo
  • jq 是否已安装并在 PATH 上可用?

    我正在尝试读取 gitlab CI 中的 yaml 文件 我安装了jq 1 0 2 and yq 2 10 1 using pip在我的 CI 工作中 我使用 python 3 7 图像 当我尝试读取 yaml 文件时 出现错误 yq Er
  • 如何为 S3 对象创建 java OutputStream 并向其写入值?

    使用 AmazonS3 类中的方法将内容添加到 S3 文件的现有方法有 通过带有输入流的 putObject 创建包含内容的本地文件并将其上传到 S3 有没有办法为现有的 S3 对象创建一个 OutputStream 可以将列表中的值写入其
  • 当 SCREEN_ON 接收器被触发时,如何禁用键盘防护并向用户显示活动?

    当 screen on 激活广播接收器时 如何禁用键盘保护 以便当它发生时用户可以看到我在其后面启动的活动 活动已经开始 我一直在尝试从屏幕关闭触发的广播接收器中执行以下代码 KeyguardManager myKeyGuard Keygu
  • .NET 正则表达式中的 \d 与 [0-9] [重复]

    这个问题在这里已经有答案了 我经常看到 0 9 用于 Stack Overflow 上的 NET 正则表达式答案 而不是 d 我问过为什么 答案往往是 d匹配的不仅仅是 0 9 那么它还匹配什么呢 这张表说它匹配十进制数字 那么呢 p Nd
  • Maven 安装插件:参数文件丢失或无效

    我有一个本地 jar 我想在我的项目中使用它 有很多方法可以做到这一点 只需手动安装到本地存储库中 使用父 pom 中的脚本进行安装 使用系统范围 使用本地存储库声明 我决定使用 Maven Install 插件将 jar 安装到存储库中
  • gcc 编译标志

    谁能解释一下为什么我们在 gcc 中使用 m 标志 我运行 man gcc 命令结果很不清楚 任何人都可以解释一下这个标志在编译中的作用吗 如何使用 m 标记其正确的语法 我需要它 因为我正在通过 make 命令运行自动生成的 makefi
  • Web API ModelBinders - 如何以不同方式绑定对象的一个​​属性

    我有以下操作签名 ValidateInput false public HttpResponseMessage PostParam Param param Param 看起来像这样 public class Param public int
  • 非静态方法 PEAR::isError() 不应静态调用

    从 RHEL 5x 升级到 CentOS 6x 后 我开始在 httpd 日志中看到以下错误 PHP 严格标准 非静态方法 PEAR isError 不应该 在 web sites blah somescript php 第 33 行静态调
  • 为什么 strncpy 被标记为不安全?

    我收到警告 warning C4996 strncpy This function or variable may be unsafe Consider using strncpy s instead To disable deprecat
  • 如何有选择地合并或选择 Git 中另一个分支的更改?

    我正在一个新项目上使用 Git 该项目有两个并行的 但目前是实验性的 开发分支 master 导入现有代码库以及我通常确定的一些修改 exp1 实验分支 1 exp2 实验分支 2 exp1 and exp2代表了两种截然不同的架构方法 在
  • 为什么 byte += 1 可以编译,而 byte = byte + 1 不能编译?

    如果我有一个字节变量 byte b 0 为什么以下工作有效 b b 1 compiles 但这不 b b 1 compile error 编译器首先理解为byte第二个是int EDIT 我知道选角 但我想提请你注意b b 1 and b
  • 如何在Flutter中使用Dart http包指向localhost:8000?

    我正在关注Flutter 网络 HTTP 教程向在我的 localhost 8000 上运行的服务器发出 GET 请求 通过浏览器访问我的本地主机工作正常 我的代码如下所示 var url http localhost 8000 Futur
  • 如何将 puppeteer-core 与 electro 结合使用?

    我从另一个 Stackoverflow 问题中得到了这段代码 import electron from electron import puppeteer from puppeteer core const delay ms number
  • max_input_vars 设置即使在 php.ini 中注释

    我的 PHP 应用程序有一个奇怪的问题 在我的 php ini 中 我已经注释掉了max input vars指令 但是当提交 1 2k 表单时 我知道它有点大 最后 200 个帖子值被截断 奇怪的是我什至重新启动了我的 Apache 我的
  • 不同 .config 文件上的 AppSettings 未更新

    我正在尝试做与最近提到的完全相同的事情上一个问题 本质上 情况是这样的 这与我的情况完全相同 我的计划是拥有这些 appSettings 在他们自己的文件 Settings config 中 我将授予修改权 Web进程用户的权限 帐户 并存
  • 在 C++ 中将数组的所有元素初始化为一个默认值?

    C 笔记 数组初始化有一个关于数组初始化的很好的列表 我有一个 int array 100 1 期望它充满 1 但事实并非如此 只有第一个值是 其余的都是 0 与随机值混合 The code int array 100 0 工作正常并将每个
  • android 在同一个活动中添加两个工具栏?

    我有一个底部菜单作为工具栏 我需要在顶部添加另一个菜单作为操作栏 这是活动的代码 public class ListViewPharms extends AppCompatActivity public Toolbar mToolbar L