繁体   English   中英

如何在PreferenceActivity的顶部显示操作栏菜单项?

[英]How to show Actionbar menu item on top in PreferenceActivity?

帮助我在操作栏顶部显示“操作栏”菜单项。 不显示溢出下拉菜单列表。请给我一个建议这样做吗?

在此处输入图片说明

在您的活动中使用它

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);

    LayoutParams lp = new LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
    View customNav = LayoutInflater.from(this).inflate(R.layout.img, null);
   EditText et = (EditText) customNav.findViewById(R.id.et);
    temppaths = new ArrayList<String>();
    Button iv = (Button) customNav.findViewById(R.id.iv);
    iv.setClickable(true);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });
    actionBar.setCustomView(customNav, lp);
    actionBar.setDisplayShowCustomEnabled(true);

和xmlLayout命名为img.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:gravity="right" >
 <EditText android:id="@+id/et" 
   android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_marginRight="20dp"
    android:gravity="center"
    android:layout_gravity="right"
    android:hint="info@xxxxxx, 1800xxxxx"
    android:background="#FF8000" />
<Button 
    android:id="@+id/iv"
    android:layout_gravity="right"
    android:layout_width="150dp"
    android:layout_height="match_parent" 
    android:layout_marginRight="20dp"
   android:text="Search"
    android:background="#FF8000"   />
</LinearLayout >

好吧

在您的活动中添加这些方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, 0, 0, "LogOut").setShortcut('0', 'o')
            .setIcon(android.R.drawable.ic_menu_edit);
    menu.add(0, 1, 0, "Save").setShortcut('1', 's')
            .setIcon(android.R.drawable.ic_menu_save);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 0:
        Toast.makeText(getApplicationContext(), "Log out clicked", 0).show();
        // save();
        // showNewOrOpenDialog();
        break;
    case 1:
        Toast.makeText(getApplicationContext(), "Save", 0).show();
        // save();
        break;
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM