如何设置SearchView TextSize?

tet*_*ius 22 android searchview

我有一个SearchView内部LinearLayout,我试图TextSize为它设置一个不同的(但SearchView)当设置android:textSize="60sp"为例时,没有任何反应.

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:padding="2dp">
<SearchView
        android:id="@+id/searchView"
        android:layout_width="350dp"
        android:textSize="60sp"
        android:layout_height="80dp"
        android:layout_marginTop="15dp"/>
<ListView
        android:id="@+id/search_results"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

有谁知道如何修改TextSize控件?

小智 44

您可以使用主题实现此目的:

在styles.xml中

<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
        <item name="android:textSize">60sp</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

并使用SearchView

<android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="350dp"
            app:theme="@style/AppSearchView"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"/>
Run Code Online (Sandbox Code Playgroud)

  • 也许是更好的解决方案 (3认同)

Pon*_*pat 36

您可以使用代码执行这些操作

SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
autoComplete.setTextSize(60);
Run Code Online (Sandbox Code Playgroud)

这是另一种解决方案

SearchView searchView = new SearchView(context);
AutoCompleteTextView search_text = (AutoCompleteTextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
search_text.setTextColor(Color.WHITE);
search_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.text_small));
Run Code Online (Sandbox Code Playgroud)

  • 第二种方法的NullPointerException (3认同)
  • 使用子线性布局的第一种方法对我来说非常有效.无论出于何种原因,使用getIdentifier()的第二个方法都会产生null search_text.无论哪种方式,谢谢你的答案,因为这正是我需要减少字体大小的一点 (2认同)

sno*_*lax 7

我希望我的解决方案会有所帮助(AndroidX)。

样式文件

 <style name="SearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:textSize">20sp</item>
 </style>
Run Code Online (Sandbox Code Playgroud)

your_layout_name.xml

<androidx.appcompat.widget.SearchView
      app:defaultQueryHint="Search"
      android:theme="@style/SearchViewStyle" // Not style!!
      .
      .
      .
         />
Run Code Online (Sandbox Code Playgroud)