背景
当我使用资源数组填充微调器时,一切正常:
Spinner countryCodeSpinner = (Spinner) findViewById(R.id.country_code_spinner);
countryCodeSpinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.country_codes_array, android.R.layout.simple_spinner_item);
从资源中读取国家/地区代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="country_codes_array">
<item>971</item>
<item>961</item>
<item>628</item>
<item>193</item>
<item>477</item>
</string-array>
</resources>
问题
但是当我像这样通过 api 调用填充微调器内容时:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
..
Spinner currencySpinner = (Spinner) findViewById(R.id.currency_spinner);
populateCurrency(currencySpinner);
private void populateCurrency(final Spinner spinner) {
_currencies = new HashMap<String, String>();
final Context context = this;
// getting the contents of spinner from api call
new SyncHelper().get(CreateOrderActivity.this,
Connections.CURRENCIES(CreateOrderActivity.this),
null, null, new SyncListener() {
@Override
public void onSuccess(String data, int requestCode) {
try {
JSONObject object = new JSONObject(data);
if (object.getString("errors") == "false") {
JSONArray currenciesArray = object.getJSONObject("data")
.getJSONArray("currencies");
for (int i=0; i< currenciesArray.length(); i++) {
String currencyRef = currenciesArray.getJSONObject(i).getString("ref");
String currencyId = currenciesArray.getJSONObject(i).getString("id");
_currencies.put(currencyRef, currencyId);
}
String[] currencyrefs = Arrays.copyOf(_currencies.keySet().toArray(),
_currencies.keySet().toArray().length, String[].class);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,currencyrefs);
spinner.setAdapter(adapter);
然后旋转器渲染的 View 全部缩小,如下所示:
即使在这两种情况下,我们都使用android.R.layout.simple_spinner_item
..知道如何解决这个问题吗?
最佳答案
我无法重现该问题,对我来说,第一个示例给出了与第二个示例类似的输出,第二个示例给出了相同的输出(没有边距,高度设置为wrap_content,同样对于第二个示例,我只使用相同的适配器构造函数来模拟)。行为差异可能是由于 API 不同(我使用了 API 26 的模拟器)。
但是,对我来说,您的第一个和所需的输出看起来像 simple_spinner_dropdown_item
而不是 simple_spinner_item
,并且我能够使用 simple_spinner_dropdown_item
生成类似的旋转器,因此您可能想检查一下。
关于java - 为什么在 Android 中异步调用时微调 View 会缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46633548/