как вертикально центрировать текст подсказки запроса в окне поиска

#android #android-studio #searchview

Вопрос:

Я установил пользовательский шрифт в своем тексте SearchView, но после этого текст подсказки для запроса немного поднялся вверх, как я его центрирую по вертикали??

введите описание изображения здесь

мой xml-файл

 <RelativeLayout
    android:id="@ id/relativelayout1"
    android:layout_width="@dimen/_250sdp"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/_8sdp"
    android:layout_marginLeft="@dimen/_16sdp"
    android:layout_marginRight="@dimen/_8sdp"
    android:background="@drawable/searchview_bg"
    app:layout_constraintBottom_toTopOf="@id/relativelayout2"
    app:layout_constraintEnd_toStartOf="@id/notification_icon"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.SearchView
        android:id="@ id/search_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        app:iconifiedByDefault="false"
        app:queryBackground="@android:color/transparent"
        app:queryHint="Search"
        app:searchIcon="@drawable/ic_search" />

    <ImageView
        android:id="@ id/filter_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_filter"
        android:layout_marginRight="@dimen/_8sdp"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>
 

мой файл java

     Typeface tf = ResourcesCompat.getFont(getContext(),R.font.poppins);
    searchView = getActivity().findViewById(R.id.search_bar);
    searchText = (TextView)searchView.findViewById(androidx.appcompat.R.id.search_src_text);
    searchText.setTypeface(tf);
 

Ответ №1:

Это может быть связано с тем, что у вашего шрифта есть это нижнее заполнение. Вы пробовали:

 android:includeFontPadding="false"
 

Другой возможной проблемой может быть гравитация

 android:gravity="center"
 

Комментарии:

1. Я использовал searchText.setIncludeFontPadding(false); java, и это сработало для меня, спасибо:)

2. Да! Это был бы программный способ сделать это!