2017-02-22 58 views
0

我正在嘗試在Android Studio上爲我的應用程序定製我的搜索欄。我想要達到的結果是這裏的一個:My Search bar and the Quora's one樣式搜索在Android上查看(min21)

我試圖按照G的文檔(https://developer.android.com/guide/topics/search/search-dialog.html)沒有結果。

這裏是我的代碼:

Styles.xml

<style name="CustomSearchViewStyle" parent="Widget.AppCompat.SearchView"> 
     <item name ="voiceIcon">@drawable/ic_flag</item> 
     <item name="searchIcon">@drawable/ic_search</item> 
     <item name="queryBackground">@color/DarkYellow</item> 
     <item name="android:background">@color/MainYellow</item> 
     <item name="android:paddingBottom">20dp</item> 
     <item name="android:paddingTop">5dp</item> 
     <item name="android:paddingStart">-15dp</item> 
     <item name="android:paddingEnd">15dp</item> 
     <item name="android:inputType">textCapWords</item> 
    </style> 

Searchable.xml

<?xml version="1.0" encoding="utf-8"?> 

    <searchable 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:label="@string/app_name" 
     android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" 

    /> 

MainActivity.xml

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

     <android.support.v7.widget.SearchView 
      android:id="@+id/top_search_bar" 
      style="@style/CustomSearchViewStyle" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/search_bar_height" 
      android:layout_alignParentStart="true" 
      app:iconifiedByDefault="false" 
      app:queryHint="@string/search_hint" 
      android:layout_alignParentEnd="true" /> 
    </RelativeLayout> 

無法獲取原因,我不能放置自定義圖標在查詢字段內部的條形末尾處,查看「提示文本」和語音搜索圖標。

我試圖激活此屬性: <item name="searchHintIcon">@drawable/ic_search</item> 但沒有任何反應。

你有什麼建議嗎?

非常感謝。

祝您有愉快的一天。

UPDATE:看來,刪除在XML或Java文件中的字符串「setIconifiedByDefault」顯示提示圖標,但我希望有我的搜索欄始終不圖標化(始終可見),因此,不解決我的問題。

回答

0

好吧,似乎我們已經找到了解決我們問題的方法。 (感謝我的隊友Claffolo)

我真的不知道如何解決我的問題,所以我們寧願創建我們的個人搜索欄,而不是使用Google的。

這是我們取得的成果:Search Bar(我用我們的標誌取代了我們的標誌,因爲我們更喜歡隱藏我們的標誌。),與我想要的很相似,呃?

這是我activity_main.xml中

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:background="?attr/colorPrimary" 
     android:layout_alignParentStart="true" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="2dp" 
      android:paddingEnd="16dp"> 

      <ImageView 
       android:id="@+id/search_bar_hint_icon" 
       android:layout_width="32dp" 
       android:layout_height="32dp" 
       android:src="@drawable/ic_search"/> 

      <EditText 
       android:id="@+id/search_bar_edit_text" 
       android:layout_width="0dp" 
       android:hint="@string/search_hint" 
       android:textColorHint="@color/LightYellow" 
       android:backgroundTint="@color/DarkYellow" 
       android:background="@color/DarkYellow" 
       android:inputType="textCapWords" 
       android:layout_weight="1" 
       android:layout_height="32dp" /> 

      <ImageButton 
       android:id="@+id/search_bar_voice_icon" 
       android:layout_width="32dp" 
       android:layout_height="32dp" 
       android:background="@drawable/ic_mic" /> 

     </LinearLayout> 

    </android.support.v7.widget.Toolbar> 

    <ImageView 
     android:id="@+id/imageview_logo" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:layout_below="@id/my_toolbar" 
     android:src="@drawable/ic_home" 
     android:background="@color/MainYellow" 
     /> 
</RelativeLayout> 

所以你可以看到它是由三個要素的結構:一個的EditText,它處理的輸入,ImageView的有表現通過搜索提示文本框前的鏡頭圖標和處理voice_speech事件的mic_icon按鈕。他們都在linearLayout,並在一個工具欄的地方,我不得不layout_weight = 1字段在EditText中實現您可以在圖片中看到的結果。

總之,這裏是我們如何處理我們的代碼:

MainActivity。java的

@Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //SETUP TOOLBAR 
     Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(myToolbar); 
} 
    voice_search_button = (ImageButton)findViewById(R.id.search_bar_voice_icon); 
    editText = (EditText) findViewById(R.id.search_bar_edit_text); 
    editText.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      fragmentManager.beginTransaction().replace(R.id.fragment_container, new SearchResultsFragment()).commit(); 
     } 
    }); 

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus){ 
       fragmentManager.beginTransaction().replace(R.id.fragment_container, new SearchResultsFragment()).commit(); 
      } 
     } 
    }); 

要處理voice_speech事件,加入MainActivity.java以下,下的onCreate overrided方法:

//Voice Search Listener 
     voice_search_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       promptSpeechInput(); 
      } 
     }); 

,並覆蓋這兩種方法:

/** 
* Showing google speech input dialog 
* */ 
private void promptSpeechInput() { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

/** 
* Receiving speech input 
* */ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
     case REQ_CODE_SPEECH_INPUT: { 
      if (resultCode == RESULT_OK && null != data) { 

       ArrayList<String> result = data 
         .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       editText.setText(result.get(0)); 
       //TODO AVVIARE ATTIVITA' CON TESTO RESULT.GET(0) 
      } 
      break; 
     } 

    } 
} 

最後,我們仍然在處理instantSearch和過濾功能,這要歸功於這位回答此問題的人:How to filter a RecyclerView with a SearchView

希望這有助於我,我會最終更新我的答案。

祝您有愉快的一天。