我正在使用微调器选择数量来重新计算配方成分的数量。 我有一个水平的LinearLayout,我添加一个标签(TextView)来描述微调器,然后是微调器。
当我第一次加载活动时,标签的位置略高于微调器,当我选择一个项目时,它会校正它的高度。
我该如何解决?
我的微调器加载方法的代码:
// Horizontal LinearLayout for displaying label + spinner
LinearLayout spinnerLayout = new LinearLayout(getApplicationContext());
spinnerLayout.setOrientation(LinearLayout.HORIZONTAL);
spinnerLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
// Textview for description
TextView lblAmount = new TextView(getApplicationContext());
lblAmount.setText("Quantity:");
lblAmount.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
lblAmount.setTextColor(Color.parseColor("#161618"));
spinnerLayout.addView(lblAmount);
// add spinner
ArrayList<Integer> spinnerArray = new ArrayList<Integer>();
for(int i = 1; i<= 10; i++) {
spinnerArray.add(i);
}
final Spinner spnIngredientQuantity = new Spinner(this);
ArrayAdapter<Integer> spinnerArrayAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spnIngredientQuantity.setAdapter(spinnerArrayAdapter);
spnIngredientQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
spinnerLayout.addView(spnIngredientQuantity);
if(firstLoad) {
spnIngredientQuantity.setSelection(spinnerArrayAdapter.getPosition(recipe.getDefaultAmount()));
firstLoad = false;
}
spnIngredientQuantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
reloadIngredients((int) spnIngredientQuantity.getSelectedItem());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Use default Quantity
}
});
scrollLayout.addView(spinnerLayout);
我认为可能是因为我将spinnerLayout的高度设置为WRAP_CONTENT,因此当TextView进入时它会更小。稍后,微调器占用更多空间,但textView不会获得更新。
这可能是问题吗?
答案 0 :(得分:0)
我不知道究竟是什么导致了这个问题,但我修复了它,将textView水平居中:
spinnerLayout.setGravity(Gravity.CENTER_VERTICAL);