簡體   English   中英

自定義動作欄視圖布局中的Android搜索

[英]Android search in custom actionbar view layout

我使用布局文件夾中的自定義布局來設置操作欄的customview

以下是操作欄布局的代碼。

custom_actionbar_home_page.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60.0dip"
android:background="@drawable/b_header_strip" >

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toLeftOf="@+id/search_btn"
    android:layout_toRightOf="@+id/imageView1"
    android:gravity="center"
    android:paddingRight="15.0dip"
    android:text="Dreamrun"
    android:textAppearance="?android:textAppearanceLarge"
    android:textColor="#ffffffff"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="35.0dip"
    android:layout_height="35.0dip"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8.0dip"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/search_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/love_btn"
    android:background="@null"
    android:src="@drawable/ic_search_header"
    android:actionViewClass="android.widget.SearchView"/>

<ImageButton
    android:id="@+id/love_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:src="@drawable/ic_whishlist" />

</RelativeLayout>

以下是onCreate方法中的Activity代碼:

actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        Singleton.getInstance(getApplicationContext());


        View localView =          LayoutInflater.from(this).inflate(R.layout.custom_actionbar_home_page, null);
        mTitleTextView = ((TextView)localView.findViewById(R.id.title_text));
        mTitleTextView.setText("DREAMRUN");
        ImageButton localImageButton = (ImageButton)localView.findViewById(R.id.search_btn);


        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) localView.findViewById(R.id.search_btn);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);


        actionBar.setCustomView(localView);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME); 

我的問題是,當我單擊search_btn時,顯示編輯文本以輸入搜索關鍵字。 它應該與從菜單中搜索相同。

但是在我的Activity我正在使用菜單文件作為操作圖標。 我正在為actionbar使用自定義布局。

請給我一個解決方案,以通過自定義布局(actionbar)實現搜索功能,而無需菜單文件。

謝謝。

您不能將“ ImageButton”轉換為“ SearchView”

final SearchView searchView (SearchView)localView.findViewById(R.id.search_btn);

看到這個鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM