在开发的这一部分,我试图通过单击按钮在屏幕上添加线性布局。我试图使它与生成的 XML 相似,但它的代码并不相同。屏幕截图在这里。EditText 不显示,也没有顶部填充。在Java是布局之后,我基于它
仅供参考:前两个布局是我对这个 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>