【问题标题】:Appearance issue in spinners in Android studioAndroid Studio 中微调器的外观问题
【发布时间】:2018-04-19 20:36:25
【问题描述】:

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

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

您对原因或解决方案有任何想法吗?

<?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) {
            }
        });
    }
}

【问题讨论】:

  • 你能发布你的部分代码和xml文件吗?
  • 我贴了xml,java代码很长不知道具体应该贴哪一部分
  • 你能把设置 Spinner 列表适配器的部分 java 代码贴出来吗?
  • @Ruan_Lopes 请向下滚动上面的代码,设置适配器的部分在那里

标签: android spinner android-spinner android-theme appearance


【解决方案1】:

我重新创建了您的代码,它对我来说很好用。

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

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 吗?

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


更新

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

使用正确的上下文与这些行为中的另一种行为相关联。虽然框架不会抱怨,并且会从使用应用程序上下文创建的 LayoutInflater 返回完美的视图层次结构,但在此过程中不会考虑您应用程序的主题和样式。这是因为 Activity 是清单中定义的主题实际附加的唯一上下文。任何其他实例都将使用系统默认主题来扩展您的视图,从而导致您可能没想到的显示输出。

来自此链接的引用https://possiblemobile.com/2013/06/context/

默认情况下,下拉视图会根据 {@link Context} 传递给适配器的构造函数。更多......

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

【讨论】:

  • 我不能使用 this 因为我在方法中使用的代码取决于块 staticSpinner.setOnItemSelectedListener
  • getApplicationContext() 替换为 MainInfoActivity.this 我认为在你的情况下它应该可以工作。
  • 我用什么替换它? 这个在那里不起作用
  • 使用 MainInfoActivity.this 代替 getApplicationContext()
【解决方案2】:

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

这基本上包括为上部微调器和底部微调器创建自己的主题。检查这是否与您的问题密切或相关。如果不上传添加微调器的活动,则重新创建情况。

【讨论】:

  • 我查看了链接,不适合我。我发布了处理微调器的代码部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多