如何将可点击图标添加到 Android 上 activity 栏的最左角?

How to add clickable icon to leftmost corner of activity bar on Android?

我想知道如何在操作栏的一角创建一个可点击的图标。我想出了如何在角落添加 应用程序图标,但它不可点击,并且在打开应用程序后出现明显的滞后时间。

在这一点上,我希望能够在那里放置 任何 图标,而不仅仅是应用程序图标。我还希望图标可以 clickable 并最终可以选择在其位置放置 activity ,例如刷新页面或返回顶部,就像 twitter应用程序。我将附上我的应用程序和 Twitter 应用程序的屏幕截图,让您了解我的要求。

按我想要的按钮的 Twitter 应用程序图片:

我的应用程序的图片,带有我添加的 activity 按钮,但不在左下角:

我不确定这能否完美地回答您的问题,但最近我创建了一个自定义工具栏,它在左侧、中间和右侧都有按钮。
因为我只有一个 Activity,所以我把这个 XML 放在了我的 MainActivity 布局中。如果你想让它在每个 Activity 之上,它可能不聪明..

在 Activity xml 中,作为第一个元素使用:

<android.support.v7.widget.Toolbar
    android:id="@+id/main_screen_top_toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:contentInsetStart="0dp"
    app:contentInsetStart="0dp"
    android:theme="@style/MainToolBarTheme"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_settings"
        android:layout_height="75dip"
        android:layout_width="75dip"
        android:layout_gravity="left"
        android:background="@drawable/icon_settings" />

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_logo"
        android:layout_height="70dip"
        android:layout_width="120dip"
        android:layout_gravity="center"
        android:background="@drawable/icon_app" />

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_social"
        android:layout_height="80dip"
        android:layout_width="70dip"
        android:layout_gravity="right"
        android:background="@drawable/icon_social" />

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

在style.xml中-将App主题设置为NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>

<style name="MainToolBarTheme">
    <item name="android:background">@android:color/white</item>
    <item name="android:layout_height">100dp</item>
</style>

在您的代码中(.java 文件)- 您可以像这样引用按钮:

ImageButton settingsButton = (ImageButton) v.findViewById(R.id.main_screen_top_toolbar_settings);

settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Do whatever...
        }
    });

将 ImageButton 的 icons/ids 更改为您的图标和 ID。 希望这对您有所帮助,如果您需要更多帮助,请告诉我。

检查下面的代码。希望它对你有用,

在你的 XML: 1. 将工具栏添加到布局文件的顶部。

<LinearLayout> 
......
......
...  
<android.support.v7.widget.Toolbar
 android:id = "@+id/toolbar"
 xmlns:local = "http://schemas.android.com/apk/res-auto"
 android:layout_width = "match_parent"
 android:layout_height = "wrap_content"
 android:background = "@color/colorPrimary"
 android:minHeight = "?attr/actionBarSize"
 local:popupTheme = "@style/ThemeOverlay.AppCompat.Light"
 local:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

..... 
.........
</LinearLayout>

然后在你的Activity中写下下面的代码onCreate。确保扩展 AppCompat Activity.

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

setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
  getSupportActionBar().setDisplayShowHomeEnabled(true);
  toolbar.setNavigationIcon(R.drawable.ic_menu);  // Replace with your icon
}

在您的 style.xml 中,将以下代码添加到您的应用主题中,

<item name = "windowNoTitle">true</item>
<item name = "windowActionBar">false</item>

这绝对有效。尝试.. 编码愉快..!!