Android 工作室中旋转器的外观问题

Appearance issue in spinners in Android studio

我在同一个 activity 中放置了两个微调器,一个用于城市,另一个用于城镇。当用户选择城市时,城镇微调器应根据所选城市填充项目。

我的问题是,背景和文字的颜色总是与第一个不同,但是它们具有相同的样式和属性。我找不到任何合乎逻辑的解决方案,也没有在网络上找到任何建议。

您知道原因或解决方案吗?

<?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="match_parent"
    android:orientation="horizontal">

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

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="20dp"
            android:onClick="pickDate"
            android:text="Select date" />

        <TextView
            android:id="@+id/viewDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Distribution date"
            android:textAlignment="center" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="The governorate"
            android:textAlignment="center" />

        <Spinner
            android:id="@+id/static_spinner"
            style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="The district"
            android:textAlignment="center" />

        <Spinner
            android:id="@+id/district_spinner"
            style="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="Place ID" />

        <EditText
            android:id="@+id/plcID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="" />

        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:onClick="next"
            android:text="Next" />

    </LinearLayout>
</LinearLayout>

MainInfoActivity 文件

public class MainInfoActivity extends Activity {
    TextView textView, plcID;
    Spinner staticSpinner;
    Spinner dynamicSpinner;
    Spinner districtSpinner;
    CharSequence[] arrayDistrict;
    ArrayAdapter<CharSequence> districtAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_info_activity);

        textView = (TextView) findViewById(R.id.viewDate);
        plcID = (TextView) findViewById(R.id.plcID);

        //Drop down lists

        staticSpinner = (Spinner) findViewById(R.id.static_spinner);

        // Create an ArrayAdapter using the string array and a default spinner
        final ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(
                this, R.array.governorate_array, android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        staticSpinner.setAdapter(staticAdapter);

        districtSpinner = (Spinner) findViewById(R.id.district_spinner) ;

        districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.Anbar, android.R.layout.simple_spinner_item);

        districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        staticSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view1, int i, long l) {

                districtAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.Anbar, android.R.layout.simple_spinner_item);

                districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // Apply the adapter to the spinner
                districtSpinner.setAdapter(districtAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
}

我本来打算要求更多代码,但后来我在另一个可能对你有用的问题中找到了这个类似的解决方案:

主要包括为上部微调器和底部微调器创建您自己的主题。检查这是否与您的问题接近或相关。如果不上传添加微调器的 activity ,以重现这种情况。

我重新创建了您的代码,它对我来说工作正常。

我认为唯一有意义的区别在于以下几行:

ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
            .createFromResource(this, R.array.governorate_array,
                    android.R.layout.simple_spinner_item);

districtAdapter = ArrayAdapter
            .createFromResource(getApplicationContext(),
                    R.array.Anbar,
                    android.R.layout.simple_spinner_item);

不使用 getApplicationContext() 你能尝试像在 staticAdapter 中那样使用 this 吗?

我曾经遇到过类似的情况,我的应用程序的主题与我的 activity 不同,而且小部件看起来也略有不同。


更新

我想补充一些关于我建议上述更改的原因的信息。

Using the right Context is linked to another one of those behaviors. While the framework will not complain and will return a perfectly good view hierarchy from a LayoutInflater created with the application context, the themes and styles from your app will not be considered in the process. This is because Activity is the only Context on which the themes defined in your manifest are actually attached. Any other instance will use the system default theme to inflate your views, leading to a display output you probably didn’t expect.

引自linkhttps://possiblemobile.com/2013/06/context/

By default, drop-down views are inflated against the theme of the {@link Context} passed to the adapter's constructor. More.......

ArrayAdapter 源代码: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/ArrayAdapter.java#461