2017-06-25 6 views
0

私が作成しているカスタムツールバーにSearchViewを統合しようとしています。Android SearchView searchIcon(折り畳まれたときのクラスを拡大)

要件は次のとおりです。離れてonMeasureをオーバーライドして、カスタムの適用から

1)2

は、私もそれ全幅を作るためにSearchViewをサブクラス化している

2)中心タイトルActionMenuViewsので、MaxWidthプロパティ(Integer.MAX_VALUE)、私も。この恐ろしいハックをオンラインで見つけてSearchViewコンテナのマージンを削除しました。

// Terrible hack (1) to set SearchView's main container margin to 0 
LinearLayout searchEditFrame = (LinearLayout) findViewById(R.id.search_edit_frame); // Get the Linear Layout 
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchEditFrame.getLayoutParams(); 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    params.setMarginStart(0); 
} else { 
    params.leftMargin = 0; 
} 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    params.setMarginEnd(0); 
} else { 
    params.rightMargin = 0; 
} 

searchEditFrame.setLayoutParams(params); 
// End terrible hack (1) 

Iはまた、自由(SIC)を取り、直接拡大鏡ImageViewのと同様のロジックを適用しました。

// Terrible hack (2) to remove hardcoded padding from search icon magnifying glass icon 
ImageView magIcon = (ImageView) findViewById(R.id.search_mag_icon); 

params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.WRAP_CONTENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT 
); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    params.setMarginStart(0); 
} else { 
    params.leftMargin = 0; 
} 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
    params.setMarginEnd(0); 
} else { 
    params.rightMargin = 0; 
} 
magIcon.setLayoutParams(params); 

// End terrible hack (2) 

(IDはabs_search_view.xmlからのものであり、それらが一致するように見える覚えておいてください)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/search_bar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

    <!-- This is actually used for the badge icon *or* the badge label (or neither) --> 
    <TextView 
      android:id="@+id/search_badge" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical" 
      android:layout_marginBottom="2dip" 
      android:drawablePadding="0dip" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="?android:attr/textColorPrimary" 
      android:visibility="gone" /> 

    <ImageView 
      android:id="@+id/search_button" 
      style="?attr/actionButtonStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_vertical" 
      android:focusable="true" 
      android:contentDescription="@string/abc_searchview_description_search" /> 

    <LinearLayout 
      android:id="@+id/search_edit_frame" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layoutDirection="locale"> 

     <ImageView 
       android:id="@+id/search_mag_icon" 
       android:layout_width="@dimen/abc_dropdownitem_icon_width" 
       android:layout_height="wrap_content" 
       android:scaleType="centerInside" 
       android:layout_gravity="center_vertical" 
       android:visibility="gone" 
       style="@style/RtlOverlay.Widget.AppCompat.SearchView.MagIcon" /> 

     <!-- Inner layout contains the app icon, button(s) and EditText --> 
     <LinearLayout 
       android:id="@+id/search_plate" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:layout_gravity="center_vertical" 
       android:orientation="horizontal"> 

      <view class="android.support.v7.widget.SearchView$SearchAutoComplete" 
        android:id="@+id/search_src_text" 
        android:layout_height="36dip" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:layout_gravity="center_vertical" 
        android:paddingLeft="@dimen/abc_dropdownitem_text_padding_left" 
        android:paddingRight="@dimen/abc_dropdownitem_text_padding_right" 
        android:singleLine="true" 
        android:ellipsize="end" 
        android:background="@null" 
        android:inputType="text|textAutoComplete|textNoSuggestions" 
        android:imeOptions="actionSearch" 
        android:dropDownHeight="wrap_content" 
        android:dropDownAnchor="@id/search_edit_frame" 
        android:dropDownVerticalOffset="0dip" 
        android:dropDownHorizontalOffset="0dip" /> 

      <ImageView 
        android:id="@+id/search_close_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:paddingLeft="8dip" 
        android:paddingRight="8dip" 
        android:layout_gravity="center_vertical" 
        android:background="?attr/selectableItemBackgroundBorderless" 
        android:focusable="true" 
        android:contentDescription="@string/abc_searchview_description_clear" /> 

     </LinearLayout> 

     <LinearLayout 
       android:id="@+id/submit_area" 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent"> 

      <ImageView 
        android:id="@+id/search_go_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="center_vertical" 
        android:paddingLeft="16dip" 
        android:paddingRight="16dip" 
        android:background="?attr/selectableItemBackgroundBorderless" 
        android:visibility="gone" 
        android:focusable="true" 
        android:contentDescription="@string/abc_searchview_description_submit" /> 

      <ImageView 
        android:id="@+id/search_voice_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="center_vertical" 
        android:paddingLeft="16dip" 
        android:paddingRight="16dip" 
        android:background="?attr/selectableItemBackgroundBorderless" 
        android:visibility="gone" 
        android:focusable="true" 
        android:contentDescription="@string/abc_searchview_description_voice" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

これは私が得て「Mものです。左のアクションメニュービューには、xmlで定義されたMenuItemがあり、カスタムスタイルがあります(ツールバーのテーマでactionButtonStyleプロパティを通じて適用されます)。

右のアクションメニュービューは、私は左のメニューにSearchViewを移動した場合と同じ結果がXML

経由のMenuItemが定義されsearchviewを持っています。

enter image description here

誰かがそのパディングおよび/または右マージンのsearchIconの画像を削除する方法を私に説明できますか?

PS:これは拡張されたSearchViewです。ボタンをクリックする部分を確認してください。明らかに余白なしに全幅に拡大されます enter image description here

答えて

0

OK、これは私が起こっていると思います。

お知らせこのライン

<ImageView 
     android:id="@+id/search_button" 
     style="?attr/actionButtonStyle" 

私はツールバーのactionButtonStyleが2回適用されますと思います。私はそれを深く見る時間がなかった...

コメントがあれば、私のゲストになります:)

関連する問題