【问题标题】:Android Action Bar Three Dots not displayedAndroid操作栏三个点不显示
【发布时间】:2013-11-02 11:16:33
【问题描述】:

请帮忙, 我做了一个自定义菜单(添加了支持库)(名称-> main_activity_actions.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"  >

<item
    android:id="@id/search"
    android:icon="@drawable/search"
    android:title="@string/search"
    yourapp:showAsAction="ifRoom" />
<item
    android:id="@id/view_all"
    android:title="@string/view_all"
    yourapp:showAsAction="never"/>
<item
    android:id="@+id/action_settings"
    yourapp:showAsAction="never"
    android:title="@string/action_settings"/>

现在我应该怎么做才能将 action_settings 放入三个点(操作栏),而不是硬件菜单按钮(没有任何黑客)。

主活动

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    return true;
}

好吧,我已经找到了破解方法,但是如果还有其他方法,请告诉我,
破解
将此代码放入 onCreate

 try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

为此,您需要导入

import java.lang.reflect.Field;
import android.view.ViewConfiguration;

【问题讨论】:

  • 如果你想充分发挥这种力量而不是使用动作提供者
  • 无需在您的问题中添加“谢谢”;我们只需要将答案标记为已接受。 :-)

标签: android android-actionbar


【解决方案1】:

如果你想显示三个点,不管设备菜单按钮是什么!然后你可以在你的应用程序类的 onCreate 方法中调用这个方法-

private void getOverflowMenu() {

     try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 天才。这是谷歌唯一对我有帮助的答案!
  • +1。谢谢你。我在升级到 sdk compile api 24 并在 android api 版本 14 和 16 中运行应用程序时遇到了这个问题。你解决了。
  • 它不适用于有菜单按钮的三星 Galaxy note I,但我仍然期待看到点,因为你提到了 irrespective of device menu button... 请你提出一些建议。谢谢。
【解决方案2】:

如果没有任何 hack,您无法在所有设备上都这样做。那些具有硬件菜单按钮(我不确定是否全部)的设备将使用它而不是溢出按钮(...)。

这在某种程度上是件好事。这些设备的用户习惯于按菜单按钮进入菜单。所以对他们来说,没有溢出按钮是正常的行为。

对于那些使用溢出按钮的设备,Android 将根据您在showAsAction 标签中的提示自行决定放置什么。这取决于屏幕尺寸、方向等。 This page 有一个表格显示显示了多少个图标(其余的进入溢出菜单)。

【讨论】:

  • 我不想在所有设备上都这样做,但在那些支持此功能的设备上。
  • 你不需要做任何特别的事情,但是如果你有一个使用菜单按钮的设备,你不会在没有破解的情况下把它变成 3 个点。
  • 最后一个问题,Hack 是好是坏,我的意思是它是否安全。
  • 看起来还不错,但我自己没用过,所以不能说 100%。
【解决方案3】:

请为 Display SherlokActionbar 测试此代码:

public class MainActivity extends SherlockActivity {
private com.actionbarsherlock.view.MenuItem mGoItem;
private com.actionbarsherlock.view.MenuItem mClearItem;

private static final int listSMS_ITEM_ID = 1;
private static final int Distance_ITEM_ID = 5;
private static final int About_ITEM_ID = 2;
private static final int Search_ITEM_ID = 3;
private static final int HELP_ITEM_ID = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public boolean onCreateOptionsMenu(Menu menu) {

    mGoItem = menu.add(0, HELP_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Distance_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, listSMS_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Search_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}
// @Override
public boolean onOptionsItemSelected(
        com.actionbarsherlock.view.MenuItem item) {
    // TODO Auto-generated method stub
    /* return super.onOptionsItemSelected(item); */

    switch (item.getItemId()) {
    case listSMS_ITEM_ID:

        Toast.makeText(getApplicationContext(), "listSMS", 1).show();


        return true;

    case Search_ITEM_ID:

        Toast.makeText(getApplicationContext(), " Search", 1).show();

        return true;




    case Distance_ITEM_ID:

        Toast.makeText(getApplicationContext(), "  Distance", 1).show();
        return true;



        case HELP_ITEM_ID:
            Toast.makeText(getApplicationContext(), "  HELP", 1).show();
            //

            return true;


    }

    return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多