【问题标题】:Noticeable delay when clicking on options menu单击选项菜单时出现明显延迟
【发布时间】:2020-02-14 13:45:20
【问题描述】:

当我单击工具栏中的选项按钮时,我总是注意到我的应用程序出现延迟。这可能需要一秒钟左右,但与其他 Android 应用程序(如 Firefox)相比,这一点非常明显,在我的手指拉起时,感觉选项菜单会立即膨胀,所以我知道这不仅仅是我的设备。我把一个最小的应用程序放在一起,有一个活动和两个布局:

package com.test.test_option_menu;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
}

activity_main.xml 是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.test_option_menu.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</RelativeLayout>

而menu.xml是

<?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"
    android:layout_width="wrap_content">
    <item
        android:id="@+id/action_help"
        android:title="Help"
        app:showAsAction="never" />
</menu>

但是,当我点击选项菜单时,我仍然会看到短暂的延迟!如何让它与我使用的其他应用程序一样快?我已经尝试过使用布局中包含的工具栏,就像这里一样,并创建了一个单独的 toolbar.xml 来获取

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

但我仍然看到同样的事情。有没有更轻量级的工具栏制作方法?还是预加载菜单布局?

【问题讨论】:

  • 你什么时候放置了setSupportActionBar方法?
  • 在 onCreate 中,在 setContentView 之后。对吗?
  • 是的,它是正确的,尝试从 标签中删除 android:layout_width 和 xmlns:app 行,并将 app:showAsAction="never" 替换为 android:showAsAction="never"跨度>
  • 我能够删除 layout_width 标签,但是当我更改 showAsAction 时,它说我需要使用“app”,因为我正在使用 AppCompatActivity。

标签: android performance android-layout


【解决方案1】:

在我的布局工具栏中而不是在活动的操作栏中填充菜单对我很有用。

在我的活动中,我有:

supportActionBar!!.hide()
val actionBar = view.findViewById<Toolbar>(R.id.toolbar)
actionBar.setOnMenuItemClickListener {
        when (it.itemId) {
            R.id.xxx-> {
                
            }
        }
        return@setOnMenuItemClickListener true
    }

在我的活动布局中,我有:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    style="@style/..."
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:menu="@menu/menu_searchview"
    app:title="..." />

在我的菜单中有:

<?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"
xmlns:tools="http://schemas.android.com/tools">

<item
    android:id="@+id/searchView"
    android:icon="@drawable/ic_search_black_24dp"
    android:title="@string/..."
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="always"
    tools:ignore="AlwaysShowAction" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 2020-05-26
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多