【问题标题】:How to toggle menu icon on the app bar in android studio [closed]如何在android studio中的应用栏上切换菜单图标[关闭]
【发布时间】:2016-01-05 14:19:29
【问题描述】:

Erm 我可以在应用栏中切换图标而不是使用切换按钮吗?

当我在应用栏中按下我的图标时,它会变为其他图标

这是我的 menu_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="course.examples.healthcare_application.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="1"
        android:title="@string/action_settings"
        app:showAsAction="never" />

    <!--lower value will be on the left-->
    <item
        android:id="@+id/Bluetooth_connect"
        android:icon="@drawable/ic_bluetooth_white_24dp"
        app:showAsAction="always"
        android:orderInCategory="2"
        android:title="@string/bluetooth_connect" />
</menu>

主要活动代码

 @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

        if(id == R.id.Bluetooth_connect){
            Toast.makeText(getApplicationContext(), "Turned on", Toast.LENGTH_LONG).show();

        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • @2Dee 我该如何解锁?为什么暂停?

标签: android android-studio material-design


【解决方案1】:

要实现切换效果,您可以在单击按钮时更改按钮的颜色。如果图标是可绘制的,则可以使用 tint 属性设置颜色。

在 xml 中可以使用

设置色调
android:tint="@color/icon_pressed"

在代码中可以使用

设置色调
imageView.setColorFilter(Color.argb(255, 255, 255, 255));

【讨论】:

    【解决方案2】:

    您在可绘制文件夹中的选择器中定义它们:

    button_state.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/pressed" />
        <item android:state_pressed="true" android:drawable="@drawable/initialpressed"/>
        <item android:state_checked="true" android:drawable="@drawable/on"/>
        <item android:drawable="@drawable/initial"/>
    </selector>
    

    然后像这样在你的按钮中定义它:

    android:background="@drawable/button_state"
    

    【讨论】:

    • 嗨。 erm 我需要在可绘制文件夹中创建一个新文件吗?我不太明白你。其实我没有使用按钮。我使用菜单文件夹中的图标
    • 好的。你只需尝试一个简单的教程compiletimeerror.com/2014/03/…
    • 我想要的是应用栏上的图标,而不是窗口背景上的图标
    • 请告诉我你的 onclicklistener
    • 嗨。我上传了主要活动代码。我没有使用 onclicker。我正在使用 onOptionMenuselected。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多