无法在操作栏中显示搜索图标

时间:2015-02-16 20:25:30

标签: android

我试图在操作栏的右上角显示搜索图标。 有关扩展ActionBarActivity

的PublicVideos.java,请参阅下面的代码

getoverflow设法显示溢出菜单,但不显示搜索图标。我在各自的drawables文件夹中添加了不同大小的搜索图标。

package faith.faithconnect;

import java.lang.reflect.Field;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;

public class PublicVideosActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_videos);

        centreLogo();
        getOverflowMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.faithmenu, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

            return super.onCreateOptionsMenu(menu);
    }

    private void getOverflowMenu() {
        // TODO Auto-generated method stub
        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();
        }
    }

    private void centreLogo() {
        // TODO Auto-generated method stub
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#F7CE04")));
        // getOverflowMenu();
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setCustomView(R.layout.public_videos_view);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent backhome = new Intent(this, MainActivity.class);

        startActivity(backhome);
    }

    // public void mingleSwipe(View view)
    // {
    // Intent mingleintent = new Intent(getApplicationContext(),
    // MingleActivity.class );
    // startActivity(mingleintent);
    //
    // }
    public void ratingSelected(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Try Google play
        intent.setData(Uri
                .parse("market://details?id=com.cubeactive.qnotelistfree"));
        startActivity(intent);
    }

    public void shareSelected(View view) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "body here");
        startActivity(Intent.createChooser(shareIntent, "Share Via"));

    }

}

下面的faithmenu.xml

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

      <item android:id="@+id/action_search"
              android:title="search"
              android:icon="@drawable/ic_action_search"
              android:showAsAction="ifRoom|collapseActionView"
              android:actionViewClass="android.support.v7.widget.SearchView" ></item>

    </menu>

2 个答案:

答案 0 :(得分:2)

您应该为app使用名称空间showAsAction,因此它应该是:

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

  <item android:id="@+id/action_search"
          android:title="search"
          android:icon="@drawable/ic_action_search"
          app:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.support.v7.widget.SearchView" >     </item>

</menu>

答案 1 :(得分:0)

当你的项目有 android:showAsAction =“ifRoom”如果操作栏中的项目空间不足,它将出现在动作溢出中。

您还可以使用“always”( android:showAsAction =“always”)来声明项目始终显示为操作按钮。但是,您不应该强制项目以这种方式出现在操作栏中。这样做可能会在屏幕较窄的设备上产生布局问题。最好使用“ifRoom”来请求项目出现在操作栏中,但允许系统在没有足够空间时将其移动到溢出中。