更新Spinner失去了风格

时间:2018-08-03 08:30:44

标签: java android android-spinner android-studio-3.0

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

<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,这将微调框变成了一个简单的微调框。

更新样式时如何保留样式?

1 个答案:

答案 0 :(得分:0)

好的,问题是我正在从simple_spinner_item传递android.R.layout

因此,我创建了一个新的布局资源文件(右键单击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。然后我的微调器保留了所有样式!