【问题标题】:SearchView search_plate not extending to full widthSearchView search_plate 未扩展到全宽
【发布时间】:2018-06-19 12:21:41
【问题描述】:

我的工具栏中有一个 SearchView,我是这样创建的:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    tools:context=".ui.search.SearchActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/profile_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/topbar_gradient"
        android:theme="@style/AppTheme.Dark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/topbar_gradient"
            android:contentInsetEnd="0dp"
            android:contentInsetLeft="-16dp"
            android:contentInsetRight="0dp"
            android:contentInsetStart="0dp"
            android:fitsSystemWindows="true"
            android:minHeight="?android:attr/actionBarSize"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.v7.widget.SearchView
                android:gravity="left"
                android:maxWidth="1000dp"
                android:id="@+id/search_view"
                android:background="@drawable/bg_search"
                app:iconifiedByDefault="false"
                app:goIcon="@null"
                app:voiceIcon="@null"
                app:commitIcon="@null"
                android:layout_marginEnd="@dimen/double_spacing"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </android.support.v7.widget.SearchView>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

(...)
</RelativeLayout>

显示如下:

所以 SearchView 确实扩展到全宽,但不是其中的 textview,通过使用布局检查器,我发现它位于名为 search_plate 的 LinearLayout 中,它位于名为 search_edit_frame 的 LinearLayout 中,但未扩展:

现在,尽我所能,我无法让搜索栏开始靠近后退按钮,也无法让搜索编辑文本扩展到全宽。

我根据其他答案尝试了这个:

var findViewById = searchView.findViewById<LinearLayout>(searchView.context.resources.getIdentifier("android:id/search_edit_frame", null, null))
findViewById?.layoutParams?.width = LinearLayout.LayoutParams.MATCH_PARENT

但它永远找不到具有此 ID 的视图。

有什么想法吗?

【问题讨论】:

标签: android searchview


【解决方案1】:

以下方法可以解决问题:

searchView.setMaxWidth( Integer.MAX_VALUE );

或者使用这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多