我想從我的res/layout目錄中膨脹佈局,並在我的活動類中添加一些視圖。android:將視圖添加到虛擬視圖
我的佈局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_marginTop="30dip"
android:layout_height="fill_parent" >
<Button
android:id="@+id/reg_button"
style="@style/simple_button"
android:onClick="clickButton"
android:text="@string/reg_button" />
<!-- I WANT TO PUT CONTENT HERE ! -->
</LinearLayout>
在我的Java代碼中,我試圖誇大上述佈局和添加的LinearLayout一個按鈕。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
//inflate xml
LinearLayout mainLayout = (LinearLayout) LayoutInflater.from(getBaseContext()).inflate(R.layout.regions_search, null);
//Create a new LinearLayout
LinearLayout newLinear = new LinearLayout(mContext);
newLinear.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//create a new Button
Button test = new Button(mContext);
test.setText("My button");
//first , I add button to the LinearLayout
newLinear.addView(test);
//Then, I add layout to the inflated layout
mainLayout.addView(newLinear);
//display
setContentView(mainLayout);
但是newLinear視圖(及其子項)沒有顯示出來。
你想要什麼?當你點擊'click button'時,然後顯示'My Button'? –
我只是想在活動被調用時顯示MyButton。 (在某些情況下,我的按鈕將不會顯示,這就是爲什麼我想控制在java端的顯示) – johann