简体   繁体   中英

How to show Actionbar menu item on top in PreferenceActivity?

Help me out to show Actionbar menu item on the Top of the Actionbar. Without showing on overflow drop down menulist.kindly give me a suggestion to do like that?

在此处输入图片说明

Use this in your activity

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

and xmlLayout name it 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 >

okk then

add these methods in your activity

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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