更改SearchView TextSize

时间:2017-06-01 14:24:32

标签: android

我在SearchView中实现了以下xml,文本条目已从顶部剪切,如下图所示。无论我提出什么textSize,都没有变化。

enter image description here

<LinearLayout
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="32dp"
   android:gravity="center_vertical"
   android:layout_marginTop="10dp">
     <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="12sp"/>
</LinearLayout>

4 个答案:

答案 0 :(得分:2)

  

尝试这个

您必须在此方法中传递SearchViewtextSize

private void setSearchviewTextSize(SearchView searchView, int fontSize) {
        try {
            AutoCompleteTextView autoCompleteTextViewSearch = (AutoCompleteTextView) searchView.findViewById(searchView.getContext().getResources().getIdentifier("app:id/search_src_text", null, null));
            if (autoCompleteTextViewSearch != null) {
                autoCompleteTextViewSearch.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
            } else {
                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(TypedValue.COMPLEX_UNIT_SP, fontSize);
            }
        } catch (Exception e) {
            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(TypedValue.COMPLEX_UNIT_SP, fontSize);
        }
    }

也更新你的XML

<LinearLayout
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical">
     <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"/>
</LinearLayout>

答案 1 :(得分:2)

sv= (SearchView) findViewById(R.id.search_view);
TextView searchText = (TextView)               
sv.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);

答案 2 :(得分:1)

我们可以直接在theme.xml中进行更改 它将影响整个具有搜索视图的应用程序。

例如,

    <item name="colorControlHighlight">@color/colorAccent</item>
    <item name="colorControlNormal">@android:color/black</item>
    <item name="android:editTextColor">@android:color/white</item>
    <item name="android:textColorHint">@android:color/white</item>
    <item name="android:textSize">30sp</item>

</style>

答案 3 :(得分:0)

您可以使用主题来实现:

在styles.xml中

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

并使用SearchView

<android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="350dp"
            app:theme="@style/AppSearchView" // use app:theme not in style
            android:layout_height="80dp"
            android:layout_marginTop="15dp"/>