更新Spinner将失去其风格



好的,所以我的布局文件中有一个微调器:

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/chapters"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="77dp"
android:layout_height="54dp"
android:layout_margin="10dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:background="@drawable/noborder"
android:entries="@array/chapter_one"
android:gravity="center"
android:spinnerMode="dropdown"
android:theme="@style/large_spinner"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="320dp" />

我是这样更新的:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, newChapters);
Chapters.setAdapter(arrayAdapter);

虽然这是有效的,但(微调器的内容会更新(它失去了它的风格——我猜是因为我正在传递android.R.layout.simple_spinner_item,它将微调器更改为一个简单的微调器。

更新时如何保留样式?

好的,所以问题是我从android.R.layout传递simple_spinner_item

因此,我创建了一个新的布局资源文件(右键单击layout并选择New layout resource file(,并将现有的客户微调器XML复制到其中,但添加了我的背景和主题(名为large_spinner_item.XML(:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:background="@drawable/noborder"
android:theme="@style/large_spinner"/>

正如你所看到的,我在那里留下了原始微调器的评论,这样我就知道它是从哪里来的。然后,为了使用该资源,我发现了这个页面,它帮助我尝试使用以下内容:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), R.layout.large_spinner_item, newChapters);

关键是只使用R.layout而不是android.R.layout。然后我的旋转器保持了它的风格!

最新更新