使用 Kotlin 设置 AutocompleteSupportFragment 的样式
Styling AutocompleteSupportFragment using Kotlin
我正在尝试更改我的 AutocompleteSupportFragment 字段的样式
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llSearchHolder"
android:padding="7dp">
<fragment android:id="@+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:hint="@string/Iam_going_to"
/>
</LinearLayout>
我尝试实施 但我总是会收到 Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText
错误。我正在使用 Kotlin,所以我的代码如下所示:
val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?
autocompleteFragment!!.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
((autocompleteFragment.getView()!!.findViewById(R.id.autocomplete_fragment)) as EditText).textSize = 30.0f
您需要使用以下代码
((autocompleteFragment.getView()!!.findViewById(R.id.places_autocomplete_search_input)) as EditText).textSize = 30.0f
或更多 Kotlin 方式,
autocompleteFragment.view?.findViewById<EditText>(R.id.places_autocomplete_search_input)?.textSize = 30.0f
EditText 的正确 ID 是 places_autocomplete_search_input
而不是 autocomplete_fragment
Analysis of the problem
您正在 xml
中使用片段 com.google.android.libraries.places.widget.AutocompleteSupportFragment
在查看 AutocompleteSupportFragment 片段的代码时,您可以看到它使用了布局 places_autocomplete_fragment.xml
。下面的代码
public class AutocompleteSupportFragment extends Fragment {
....
public AutocompleteSupportFragment() {
super(layout.places_autocomplete_fragment);
....
}
}
现在,如果你查看 places_autocomplete_fragment.xml
,你可以看到 EditText 的 id 是 places_autocomplete_search_input
,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:layoutDirection="locale"
android:orientation="vertical"
android:textDirection="locale">
<ImageButton
android:id="@+id/places_autocomplete_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@null"
android:contentDescription="@string/places_autocomplete_search_hint"
android:padding="@dimen/places_autocomplete_button_padding"
android:src="@drawable/quantum_ic_search_grey600_24" />
<EditText
android:id="@+id/places_autocomplete_search_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/places_autocomplete_search_hint"
android:inputType="textNoSuggestions"
android:lines="1"
android:maxLines="1"
android:paddingLeft="@dimen/places_autocomplete_search_input_padding"
android:paddingRight="@dimen/places_autocomplete_search_input_padding"
android:singleLine="true"
android:textColor="@color/places_autocomplete_search_text"
android:textColorHint="@color/places_autocomplete_search_hint"
android:textSize="@dimen/places_autocomplete_search_input_text" />
<ImageButton
android:id="@+id/places_autocomplete_clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@null"
android:contentDescription="@string/places_autocomplete_clear_button"
android:padding="@dimen/places_autocomplete_button_padding"
android:src="@drawable/quantum_ic_clear_grey600_24" />
</LinearLayout>
Android 可能会在您的 xml 文件中建议 Replace the <fragment> tag with FragmentContainerView.
。这会破坏自定义样式,因此请继续使用 <fragment>
:
使用这个:
<fragment
android:id="@+id/hostAutocompleteFrag"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
不要使用这个:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/hostAutocompleteFrag"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
这是我的 AutocompleteSupportFragment 初始化方法,供您参考:
private fun initAutoComplete() {
val mapAutocomplete = childFragmentManager.findFragmentById(R.id.hostAutocompleteFrag)
autoCompFrag = mapAutocomplete as AutocompleteSupportFragment
val hintTxt = autoCompFrag?.findViewById<EditText>(R.id.places_autocomplete_search_input)
hintTxt?.textSize = 15f
hintTxt?.setTextColor(requireContext().getColor(R.color.greyColor))
autoCompFrag.setHint(getString(R.string.search_hint))
autoCompFrag.setPlaceFields(listOf(Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS))
autoCompFrag.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
val latLng = place.latLng ?: return
setMapLocation(latLng.latitude, latLng.longitude)
}
override fun onError(status: Status) {
Log.e("$TAG initAutoComplete --- ", "An error occurred: $status")
}
})
}
用于在 kotlin 中更改 EditText 颜色
val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocompleteFragment) as AutocompleteSupportFragment?
autocompleteFragment.view?.findViewById<EditText>(places_autocomplete_search_input)?.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
如果仍然无法更改,请尝试将值中的颜色设置为
<color name="places_autocomplete_search_text">#707070</color>
钥匙很重要places_autocomplete_search_text
我正在尝试更改我的 AutocompleteSupportFragment 字段的样式
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llSearchHolder"
android:padding="7dp">
<fragment android:id="@+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:hint="@string/Iam_going_to"
/>
</LinearLayout>
我尝试实施 Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText
错误。我正在使用 Kotlin,所以我的代码如下所示:
val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?
autocompleteFragment!!.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
((autocompleteFragment.getView()!!.findViewById(R.id.autocomplete_fragment)) as EditText).textSize = 30.0f
您需要使用以下代码
((autocompleteFragment.getView()!!.findViewById(R.id.places_autocomplete_search_input)) as EditText).textSize = 30.0f
或更多 Kotlin 方式,
autocompleteFragment.view?.findViewById<EditText>(R.id.places_autocomplete_search_input)?.textSize = 30.0f
EditText 的正确 ID 是 places_autocomplete_search_input
而不是 autocomplete_fragment
Analysis of the problem
您正在 xml
中使用片段com.google.android.libraries.places.widget.AutocompleteSupportFragment
在查看 AutocompleteSupportFragment 片段的代码时,您可以看到它使用了布局 places_autocomplete_fragment.xml
。下面的代码
public class AutocompleteSupportFragment extends Fragment {
....
public AutocompleteSupportFragment() {
super(layout.places_autocomplete_fragment);
....
}
}
现在,如果你查看 places_autocomplete_fragment.xml
,你可以看到 EditText 的 id 是 places_autocomplete_search_input
,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:layoutDirection="locale"
android:orientation="vertical"
android:textDirection="locale">
<ImageButton
android:id="@+id/places_autocomplete_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@null"
android:contentDescription="@string/places_autocomplete_search_hint"
android:padding="@dimen/places_autocomplete_button_padding"
android:src="@drawable/quantum_ic_search_grey600_24" />
<EditText
android:id="@+id/places_autocomplete_search_input"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/places_autocomplete_search_hint"
android:inputType="textNoSuggestions"
android:lines="1"
android:maxLines="1"
android:paddingLeft="@dimen/places_autocomplete_search_input_padding"
android:paddingRight="@dimen/places_autocomplete_search_input_padding"
android:singleLine="true"
android:textColor="@color/places_autocomplete_search_text"
android:textColorHint="@color/places_autocomplete_search_hint"
android:textSize="@dimen/places_autocomplete_search_input_text" />
<ImageButton
android:id="@+id/places_autocomplete_clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@null"
android:contentDescription="@string/places_autocomplete_clear_button"
android:padding="@dimen/places_autocomplete_button_padding"
android:src="@drawable/quantum_ic_clear_grey600_24" />
</LinearLayout>
Android 可能会在您的 xml 文件中建议 Replace the <fragment> tag with FragmentContainerView.
。这会破坏自定义样式,因此请继续使用 <fragment>
:
使用这个:
<fragment
android:id="@+id/hostAutocompleteFrag"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
不要使用这个:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/hostAutocompleteFrag"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
这是我的 AutocompleteSupportFragment 初始化方法,供您参考:
private fun initAutoComplete() {
val mapAutocomplete = childFragmentManager.findFragmentById(R.id.hostAutocompleteFrag)
autoCompFrag = mapAutocomplete as AutocompleteSupportFragment
val hintTxt = autoCompFrag?.findViewById<EditText>(R.id.places_autocomplete_search_input)
hintTxt?.textSize = 15f
hintTxt?.setTextColor(requireContext().getColor(R.color.greyColor))
autoCompFrag.setHint(getString(R.string.search_hint))
autoCompFrag.setPlaceFields(listOf(Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS))
autoCompFrag.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
val latLng = place.latLng ?: return
setMapLocation(latLng.latitude, latLng.longitude)
}
override fun onError(status: Status) {
Log.e("$TAG initAutoComplete --- ", "An error occurred: $status")
}
})
}
用于在 kotlin 中更改 EditText 颜色
val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocompleteFragment) as AutocompleteSupportFragment?
autocompleteFragment.view?.findViewById<EditText>(places_autocomplete_search_input)?.setTextColor(ContextCompat.getColor(context, R.color.colorWhite));
如果仍然无法更改,请尝试将值中的颜色设置为
<color name="places_autocomplete_search_text">#707070</color>
钥匙很重要places_autocomplete_search_text