【问题标题】:Android SearchView searchIcon(magnifying class when collapsed) right paddingAndroid SearchView searchIcon(折叠时放大类)右填充
【发布时间】:2017-06-25 21:55:21
【问题描述】:

我正在尝试在我正在制作的自定义工具栏上集成一个 SearchView。

要求是:

1) 2 个操作菜单视图

2) 居中的标题

我还对 SearchView 进行了子类化以使其全宽,因此,除了覆盖 onMeasure 和应用自定义 MaxWidth(Integer.MAX_VALUE) 之外,我也一样。应用这个在网上找到的可怕的 hack 来删除 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 定义的 searchview MenuItem

如果我将 SearchView 移到左侧菜单,结果相同。

有人可以向我解释如何去除 searchIcon 图像的填充和/或右边距吗?

PS:这是扩展的 SearchView。请注意右侧部分,我单击按钮的位置。它显然扩展为全宽,没有边距

【问题讨论】:

    标签: android android-actionbar android-toolbar searchview


    【解决方案1】:

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

    注意这一行

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

    我认为 Toolbar 的 actionButtonStyle 被应用了两次。我还没有时间深入研究它......

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

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 2013-09-28
      • 2013-12-17
      • 2014-09-03
      相关资源
      最近更新 更多