0
在我的應用程序中,我希望動態創建xml,因爲根據一些輸入數據我想要不同的元素。所以我從這個例子開始,創建一個簡單的TextView和Spinner的動態XML。問題是我沒有在模擬器中看到任何東西。在android中創建動態xml
這裏是我的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout top = new LinearLayout(this);
top.setOrientation(LinearLayout.VERTICAL);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
top.addView(ll);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
String signs[]={"+","-"};
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, signs);
spinner.setAdapter(spinnerArrayAdapter);
ll.addView(spinner, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}