【问题标题】:Android: Spinner not working properly on 4.4.xAndroid:微调器在 4.4.x 上无法正常工作
【发布时间】:2018-11-04 12:50:59
【问题描述】:

我正在以编程方式添加 Spinner,使用自定义数组适配器类(扩展 ArrayAdapter)和我的 Spinner 的自定义布局(由 ImageView 和每行一个 TextView)。

一切正常,除了 Android Kit Kat:如果我点击我的 Spinner,它不会显示下拉项目,即使它包含正确的项目。我在 Android 6.x 和 7.x 上调试:它没有任何问题。 如果我使用自定义适配器和布局使用膨胀布局(在我的活动的 XML 内部),我没有任何问题,但如果我以编程方式添加我的 Spinner(使用外部 XML 布局),它就不起作用。

您知道 Android 4.4.x 中是否存在关于 Spinner/Custom Adapter 的已知兼容性问题? (如果有用我可以加代码)

编辑

我的 Activity 中的部分代码:

TableLayout container = (TableLayout)findViewById(R.id.table);
LayoutInflater inflator = this.getLayoutInflater();
//Single row I wish to add programmatically
TableLayout row = new TableLayout(getApplicationContext());
inflator.inflate(R.layout.internal_layout_to_clone, row);
container.addView(row);
//Acquire Spinner
Spinner spinner = (Spinner)row.findViewById(R.id.spinner);
//[here I use Custom Adapter to populate my Select: values are shown properly]

R.layout.internal_layout_to_clone 是一个 XML 文件,其中包含一个带有多个 TableRow(s) 的 TableLayout,其中一行包含我的 Spinner。

我不知道问题是不是我将 TableLayout 嵌套在另一个 TableLayout 中,也许这在 Android 4.4 中没有得到很好的管理

【问题讨论】:

  • 你在使用谷歌的兼容支持库吗?
  • 是的,我正在使用这个:com.android.support:appcompat-v7:22.2.0
  • 更新appcompat库看看。可能是因为旧版本。

标签: android spinner custom-arrayadapter


【解决方案1】:

我相信问题出在代码的某个地方,如果你能上传它,我会看看。 顺便说一句,我一直在为微调器使用外部库,这使得微调器的使用更容易,而且看起来比普通的更好。 https://github.com/ybq/Android-SpinKit

【讨论】:

  • 这似乎是一个不错的图书馆,我会尽快看看,谢谢!我会尽快添加代码,感谢您的帮助
  • 我刚刚上传了部分代码;我还检查了我的自定义适配器,它在我不克隆/获取布局时工作:当我在我的活动布局中获取元素时它工作,当我尝试从R.layout.internal_layout_to clone获取元素时它不工作@
【解决方案2】:

我刚刚解决了我的问题。我改变了我用来扩展外部布局的方式:

TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflator = this.getLayoutInflater();
TableLayout row = new TableLayout(getApplicationContext());
inflator.inflate(R.layout.internal_layout_to_clone, row);
table.addView(row);

到这里:

TableLayout table = (TableLayout)findViewById(R.id.table);
TableLayout row = (TableLayout)LayoutInflater.from(this).inflate(R.layout.internal_layout_to_clone, null);
table.addView(row);

我不知道为什么,可能有些涉及的方法与旧的 Android 版本不完全兼容,但现在它在每个测试的版本上都能正常工作。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多