为什么我的SearchView在Android中使用AndroidX时不显示queryHint?

huangapple go评论56阅读模式
英文:

Why does my Searchview doesn't display the queryHint in android using androidx?

问题

以下是要翻译的代码部分:

<!-- Add the Toolbar -->
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/flash_tablebar"
    android:title="Lingo"
    android:titleTextColor="@color/new_design_darker_shade">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:background="#FFFF"
        android:queryHint="Search..."
        android:textColorHint="@android:color/black"
        android:iconifiedByDefault="false"/>
</androidx.appcompat.widget.Toolbar>

<!-- NestedScrollView to contain the GridView and make it scrollable -->
<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true">

    <!-- Your GridView -->
    <GridView
        android:id="@+id/mainSpecimens"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?android:actionBarSize"
        android:clipToPadding="false"
        android:horizontalSpacing="6dp"
        android:numColumns="3"
        android:verticalSpacing="6dp" />
</androidx.core.widget.NestedScrollView>

希望这可以帮助您解决搜索视图中查询提示不显示的问题。

英文:

I have a searchView in the Toolbar which also has Gridview elements. Everything works fine except the queryHint doesn't get displayed in the searchView. I have tried setting android:iconifiedByDefault="false" but the issue persists. Below is the code of activity's layout.

firstscreen.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    tools:context=&quot;.ui.activities.FirstScreen&quot;&gt;

    &lt;!-- android:numColumns=2 is the number of columns for Grid View
         android:horizontalSpacing is the space between horizontal grid items --&gt;
    &lt;androidx.fragment.app.FragmentContainerView
        android:id=&quot;@+id/nav_host_fragment&quot;
        android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;0dp&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/bottomNavigationView&quot;
        app:defaultNavHost=&quot;true&quot;
        app:navGraph=&quot;@navigation/nav_graph&quot; /&gt;

    &lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:background=&quot;@color/bottomNavigation&quot;
        android:id=&quot;@+id/bottomNavigationView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;66dp&quot;
        app:layout_insetEdge=&quot;bottom&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.5&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:menu=&quot;@menu/bottom_navigation_menu&quot;/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And this is my fragment's xml layout where the searchview is added.
fragment_user.xml

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;!-- Add the Toolbar --&gt;
    &lt;androidx.appcompat.widget.Toolbar
        android:id=&quot;@+id/toolbar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@drawable/flash_tablebar&quot;
        android:title=&quot;Lingo&quot;
        android:titleTextColor=&quot;@color/new_design_darker_shade&quot;&gt;

        &lt;androidx.appcompat.widget.SearchView
            android:id=&quot;@+id/searchView&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginStart=&quot;50dp&quot;
            android:layout_marginEnd=&quot;40dp&quot;
            android:layout_marginTop=&quot;20dp&quot;
            android:layout_marginBottom=&quot;10dp&quot;
            android:background=&quot;#FFFF&quot;
            android:queryHint=&quot;Search...&quot;
            android:textColorHint=&quot;@android:color/black&quot;
            android:iconifiedByDefault=&quot;false&quot;/&gt;
    &lt;/androidx.appcompat.widget.Toolbar&gt;

    &lt;!-- NestedScrollView to contain the GridView and make it scrollable --&gt;
    &lt;androidx.core.widget.NestedScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_weight=&quot;1&quot;
        android:fillViewport=&quot;true&quot;&gt;

        &lt;!-- Your GridView --&gt;
        &lt;GridView
            android:id=&quot;@+id/mainSpecimens&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;?android:actionBarSize&quot;
            android:clipToPadding=&quot;false&quot;
            android:horizontalSpacing=&quot;6dp&quot;
            android:numColumns=&quot;3&quot;
            android:verticalSpacing=&quot;6dp&quot; /&gt;
    &lt;/androidx.core.widget.NestedScrollView&gt;
&lt;/LinearLayout&gt;

答案1

得分: 1

app:queryHint="Search..."

app:iconifiedByDefault="false"

英文:

you should change tag of queryHint and iconifiedByDefault form android to app.

app:queryHint="Search..."

app:iconifiedByDefault="false"

huangapple
  • 本文由 发表于 2023年7月17日 23:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定