【问题标题】:Overflow dots not appearing on tablet平板电脑上没有出现溢出点
【发布时间】:2015-07-07 19:53:08
【问题描述】:

由于某种原因,当我选择一个列表项时,我的主详细信息应用程序的平板电脑上没有出现溢出点,但它们却出现在我的手机上。尽管使用了必要的代码,但我不确定为什么会发生。是否有人缺少任何代码或不应该存在的代码?

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        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();
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(getResources().getString(R.string.greeting));

        FragmentMainList newFragment = new FragmentMainList();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();

        if (findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        }
    }
}

活动类

public class MyProductActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_fragment);

        if (savedInstanceState == null) {
            FragmentProduct newFragment = new FragmentProduct();
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        }

        ActionBar actionBar = getSupportActionBar(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            final Intent intent = NavUtils.getParentActivityIntent(this);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            NavUtils.navigateUpTo(this, intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

片段类

public class FragmentProduct extends android.support.v4.app.Fragment {

    public FragmentProduct() {}

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_product, container, false);

        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            final Intent intent = NavUtils.getParentActivityIntent(getActivity());
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            NavUtils.navigateUpTo(getActivity(), intent);
            return true;
        }

        int id = item.getItemId();

        if (id == R.id.action_options) {
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 您的平板电脑有菜单键?
  • 只有当您的操作栏上的项目多于一次无法显示时,才会显示溢出点。平板电脑上有更多空间
  • @jradich1234 有什么办法可以显示它们吗?

标签: android android-actionbar


【解决方案1】:

只有没有硬件菜单按钮的设备才会显示这些点。对于具有硬件菜单按钮的设备,不会显示溢出点,因为点击菜单按钮会执行相同的操作。 更多here

编辑

如果出于某种原因你真的想展示它,你可以在你的Application.onCreate()或主(启动器)活动onCreate()中添加以下代码

// force show actionbar 'overflow' button on devices with hardware menu button
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();
}

【讨论】:

  • Application.onCreate()在哪里?
  • 您的Application 文件(如果您添加了此类文件)。如果没有,请将其添加到您的启动器活动onCreate() 但在调用setContentView()之前
  • WCBankActivity 是您的主要活动吗?因此,将代码粘贴到其onCreate() 方法中。我看到它在 Note 4 中正常工作(因为它有一个菜单按钮)。如果您的平板电脑没有有硬件菜单按钮并且仍然没有看到溢出菜单 - 那么情况就不同了。平板电脑是什么制造商、品牌?
  • 好的,用 2 个带硬件按钮的模拟设备启动 GenyMotion - Note 3 和 XPeria Tablet Z。两者都有硬件按钮 - 两者都显示溢出菜单。如果您使用的是 Android 模拟器,也许您会想尝试并坚持使用GenyMotion。它和模拟器一样是免费的。
  • 好吧,不管你使用的是活动还是片段。您正在使用至少一项活动(您的启动器活动)。在您的启动器活动中添加代码。
猜你喜欢
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 2014-10-21
相关资源
最近更新 更多