如何在PreferenceActivity中显示Actionbar菜单项?

时间:2014-03-25 10:39:29

标签: android android-actionbar preferenceactivity

帮我在Actionbar的顶部显示Actionbar菜单项。没有显示溢出下降menulist.kindly给我一个建议这样做?

2 个答案:

答案 0 :(得分:0)

enter image description here

在您的活动中使用此功能

    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 >

答案 1 :(得分:0)

okk然后

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

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