【问题标题】:Adding a button to the Action Bar in Android?在 Android 的操作栏中添加一个按钮?
【发布时间】:2014-06-27 14:26:46
【问题描述】:

由于某种原因,我无法让我的按钮出现在操作栏上。我已经在 /res/menu 中的 XML 文件中定义了它,并对其进行了膨胀并监听动作。该图标位于 /res/drawable-hdpi 中。在 logcat 中没有显示任何有趣的内容。 :(

XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/logout"
      android:icon="@drawable/logout"
      android:title="Logout"
      android:orderInCategory="100"
      android:showAsAction="always" />
</menu>

主要活动中的代码:

public class MainActivity extends ActionBarActivity {

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.logout:
            //logout code
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    //rest of app
}

我按照这个问题来解决我最初的问题,但它没有帮助。 How to add button in ActionBar(Android)?

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    试试这个改变:

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    
        <item android:id="@+id/logout"
            android:icon="@drawable/logout"
            android:title="Logout"
            android:orderInCategory="100"
            yourapp:showAsAction="always" />
    </menu>
    

    【讨论】:

    • 太棒了!那成功了!有没有办法让我的按钮变成矩形而不是传统的正方形?
    • 我知道你可以通过在 item 标签中使用 android:actionLayout="@layout/layout_name" 来为菜单项设置自定义布局。但也许最好为此问另一个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多