0
我正在使用微調器來選擇重新計算配方成分數量的數量。 我有一個水平LinearLayout,我添加一個標籤(TextView)來描述微調器,然後微調。帶說明標籤不同高度的Android微調器
當我第一次加載活動時,標籤位置稍微高於微調器,當我選擇一個項目時,它會更正它的高度。
我該如何解決這個問題?
代碼我的旋裝法:
// 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不會獲得更新。
這可能是問題嗎?
您可以加入你有什麼截圖PLZ :) –
@AftabHussain添加屏幕截圖 – Marcel
我剛剛在API 16和API 23上試過了你的代碼,但是它顯示了你想要的方式:/雖然我認爲高度是問題 –