Android SearchView searchIcon(折叠时的放大类)右填充

问题描述:

我正在尝试在我自定义的工具栏上集成SearchView。Android SearchView searchIcon(折叠时的放大类)右填充

的要求是:

1)2个ActionMenuViews

2)为中心的标题

我还子类搜索查看使其整个宽度,因此,除了覆盖onMeasure和施加定制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) 

我还采取了*(原文如此)和直接应用类似的放大镜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> 

这就是我越来越。左侧的操作菜单视图有一个通过xml定义的MenuItem,并具有自定义样式(通过工具栏主题中的actionButtonStyle属性应用)。

正确的行动菜单视图具有菜单项通过XML

限定的搜索查看相同的结果是,如果我移动搜索查看到左边的菜单。

enter image description here

有人可以向我解释如何剥离其填充和/或右边缘的searchIcon形象?

PS:这是扩展的SearchView。注意正确的部分,我点击按钮。它显然扩大到全幅无边距 enter image description here

好吧,这就是我认为正在发生的事情。

声明本线

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

我认为工具栏的actionButtonStyle被涂抹两次。我没有时间去深入研究它...

如果有人有任何意见,请成为我的客人:)