如何更改 android 中 autocompletefragment 中的文字大小?

How to change text size in places autocompletefragment in android?

我在我现在正在处理的项目中使用 google 提供的 PlaceAutocompleteFragment。但问题是我需要显示我从自动完成小部件中选择的地名,但没有显示整个文本,因为小部件中的默认文本大小太大。那么,有什么方法可以在不创建自己的自定义搜索的情况下更改 PlaceAutocompleteFragment 中的文本大小 UI?

我的XML代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.baldysns.capedbaldy.materialdesigntest.activity.DrawerMainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/toolbar_drawer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <LinearLayout
                    android:id="@+id/lnr_google_searchbar"
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/btn_white_notboerder">

                    <fragment
                        android:id="@+id/place_autocompletehome_fragment"
                        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </android.support.v7.widget.Toolbar>
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.ibaax.com.ibaax.FragmentDrawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_drawer"
        tools:layout="@layout/fragment_drawer" /></android.support.v4.widget.DrawerLayout>

以及问题截图:

您的屏幕截图显示您在搜索图标和取消图标处设置了边距或填充。将其删除,以便为文本视图创建更多 space。

您还可以使用 autofittextview library(https://github.com/grantland/android-autofittextview),它会根据您的文本视图大小调整文本大小。

使用片段实例,您可以通过搜索框更改字体大小和您想要的任何内容 EditText..这就是您需要做的

// placeAutocompleteFragment - is my PlaceAutocompleteFragment instance
((EditText)placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f);

更新时间:2020 年 5 月 26 日

看起来在最新的 SDK 中视图 ID 已更改为 R.id.places_autocomplete_search_input

最简单

 ((EditText)findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f);

skadosh 的情况下,您需要先获取片段实例。

autoCompleSupportFragment 有一个 EditText 属性。可以相应地修改。 然而,editText 的名称不是很清楚。就我而言,它是 "a"

autoCompleteFragment.a.gravity = Gravity.START

所以为了修改文字大小,我做了:

autoCompleteFragment.a.textSize = 14.0f

假设您在 Javacode 中的片段是 autocompleteFragment,您可以使用

autocompleteFragment.a.setTextSize(12.0f);
View fView = autocompleteFragment.getView();

设置文本、提示颜色和大小:

EditText etTextInput = fView.findViewById(R.id.places_autocomplete_search_input);
etTextInput.setTextColor(Color.WHITE);
etTextInput.setHintTextColor(Color.WHITE);
etTextInput.setTextSize(12.5f);

设置搜索自定义图像:

ImageView ivSearch = fView.findViewById(R.id.places_autocomplete_search_button);
ivSearch.setImageResource(R.drawable.ic_search_white);

设置搜索文本清晰图像:

ImageView ivClear = fView.findViewById(R.id.places_autocomplete_clear_button);
ivClear.setImageResource(R.drawable.ic_close_white);

输出: