2012-10-12 30 views
1

我想從我的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視圖(及其子項)沒有顯示出來。

+0

你想要什麼?當你點擊'click button'時,然後顯示'My Button'? –

+0

我只是想在活動被調用時顯示MyButton。 (在某些情況下,我的按鈕將不會顯示,這就是爲什麼我想控制在java端的顯示) – johann

回答

3

使用此在您的活動,

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // display 
    setContentView(R.layout.activity_main); 

    Context mContext = getApplicationContext(); 
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.regions_search); 
    // Create a new LinearLayout 
    LinearLayout newLinear = new LinearLayout(mContext); 
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(
      LayoutParams.FILL_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); 
} 

,並在寫你的XML文件(activity_main.xml中),如下圖所示,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/regions_search" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_marginTop="30dip" 
android:orientation="vertical"> 

<Button 
    android:id="@+id/reg_button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="clickButton" 
    android:text="RegButton" /> 

<!-- I WANT TO PUT CONTENT HERE ! --> 

</LinearLayout> 

試試這個它會工作100%。

+0

在XML文件LinearLayout標籤android:orientation =「vertical」是最重要的第一次嘗試,只在佈局中添加這個。如果它不起作用,那麼嘗試所有這些代碼。 –

+1

完美!我被說服了這項任務需要通貨膨脹。非常感謝你 ! – johann

0

您可以嘗試爲新添加的LinearLayoutButton添加寬度和高度參數嗎?創建視圖時需要這些參數。

這樣子。

newLinear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

+0

謝謝。我嘗試添加布局和按鈕的參數,但不顯示按鈕。 – johann

+0

也設置爲相同的按鈕。 – midhunhk

+0

謝謝你的幫助。感謝Anil的代碼,我顯示了我的按鈕。謝謝 – johann

0

test.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1F));

註釋行

setContentView(mainLayout);

+0

謝謝。如果我評論「setContentView」代碼,屏幕上沒有任何東西出現 – johann

+0

嘗試添加setLayoutParams與重力按鈕 – mukesh

+0

謝謝你的幫助。我用Anil的代碼解決了我的問題。謝謝 – johann