1
在這部分開發中,我嘗試在按鈕單擊按鈕上將線性佈局添加到屏幕上。我試圖使它與XML生成的類似,但它沒有編碼相同。 Screen Shot here. EditText不顯示並且沒有頂部填充。之後,Java是佈局我立足其關閉的Android:由Java生成的佈局看起來不像XML
FYI:前兩名的佈局是什麼,我阿明與該Java
這是到目前爲止我的代碼:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LinearLayout.LayoutParams((getDP(80f)), LinearLayout.LayoutParams.FILL_PARENT));
TextView numberView = new TextView(this);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(getDP(80f),getDP(10f));
numberView.setLayoutParams(tvParams);
numberView.setText(numOfItems+".");
layout.addView(numberView);
EditText optionText = new EditText(this);
LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(getDP(80f),getDP(100f));
numberView.setLayoutParams(etParams);
numberView.setHint("List Option "+numOfItems);
layout.addView(optionText);
insideScroll.addView(layout);
而且XML:
<LinearLayout
android:paddingLeft="15dp"
android:weightSum="100"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_weight="10"
android:layout_width="10dp"
android:layout_height="80dp"
android:text="1."
android:id="@+id/tvItem1"/>
<EditText
android:layout_
weight="90"
android:layout_width="100px"
android:layout_height="80dp"
android:hint="List Item 1"
android:id="@+id/etItem1"
android:paddingTop="50px"/>
</LinearLayout>
爲什麼不堅持使用xml並在xml中進行整體佈局?有從Java代碼做觀點的理由嗎? – Mike