預先感謝您的幫助。TextView未出現,Android
我想將一個垂直加權的TextView對象添加到LinearLayout。
//root container
LinearLayout rootContainer = new LinearLayout(activity);
rootContainer.setOrientation(LinearLayout.HORIZONTAL);
rootContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,1f));
rootContainer.setBackgroundColor(Color.parseColor("#000000"));
//Text View
TextView instructions = new TextView(activity);
instructions.setLayoutParams(new LinearLayout.LayoutParams(0,
LayoutParams.MATCH_PARENT,.7f));
instructions.setText(survey.instructions);
instructions.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
instructions.setBackgroundColor(Color.RED);
rootContainer.addView(instructions);
當我使用上面的代碼時,我得到一個空白屏幕。但是,如果我註釋掉
instructions.setLayoutParams(new LinearLayout.LayoutParams(0,
LayoutParams.MATCH_PARENT,.7f));
TextView出現(雖然不是我想要的大小)。
從我見過的所有東西中,這是生成加權TextView(垂直加大屏幕大小)的方法。我究竟做錯了什麼?
如果你使用 'instructions.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,.7f));' –
問題解決了。雖然我不完全確定爲什麼。如果你可以給出答案並解釋爲什麼這個工作,我會接受它。 – HXSP1947
是我所做的沒有工作的原因,因爲我用0而不是0dp就像我可能有,如果我在xml中做到這一點? – HXSP1947