关于android:如何设置SearchView TextSize?

How to set SearchView TextSize?

我在LinearLayout内有一个SearchView,我试图为其设置一个不同的TextSize(SearchView),但是例如在设置android:textSize="60sp"时,什么也没有发生。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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"/>

有人知道如何修改控件的TextSize吗?


您可以使用主题实现:

在styles.xml

1
2
3
<style name="AppSearchView" parent="Widget.AppCompat.SearchView">
        <item name="android:textSize">60sp</item>
    </style>

并使用SearchView

1
2
3
4
5
6
<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"/>


您可以使用代码

完成这些操作

1
2
3
4
5
6
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);

这是另一种解决方案

1
2
3
4
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));


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

styles.xml

1
2
3
 <style name="SearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:textSize">20sp</item>
 </style>

your_layout_name.xml

1
2
3
4
5
6
7
<androidx.appcompat.widget.SearchView
      app:defaultQueryHint="Search"
      android:theme="@style/SearchViewStyle" // Not style!!
      .
      .
      .
         />

使用ActionBarSherlock时,您必须执行以下操作:

1
2
    AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
    searchTextView.setTextColor(getResources().getColor(R.color.search_text_color));


尽管AutoCompleteTextViewSearchAutoComplete的超类,但SearchView使用SearchAutoComplete而不是AutoCompleteTextView。使用它并执行更多操作。

1
2
3
4
5
6
SearchView searchView=...;
SearchView.SearchAutoComplete autoComplete= searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
float textSpSize=10.f;
if (autoComplete!=null){
    autoComplete.setTextSize(textSpSize);
   }

检查此答案我们可以限制Android中SearchView的edittext中的字符数吗?它使用SearchView.OnQueryTextListener来检查是否超出限制。