2012-01-12 44 views
1

這是我的XML代碼:XML來等效動態XML從集合類

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff"> 

    <Button 
     android:id="@+id/button_weather9" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Πίσω" /> 

    <ImageView 
     android:id="@+id/weather_icon" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal" 
     android:scaleType="fitXY" 
     android:src="@drawable/weather1" /> 
</LinearLayout> 

但在我的應用我想我的類中,與同樣的事情上面動態設定一個新的XML。我不知道參數究竟是什麼。到目前爲止,我已經寫了這個。你能幫我與它完成缺少什麼:

final LinearLayout test2 = new LinearLayout(this); 
     test2.setOrientation(LinearLayout.VERTICAL); 
     test2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     final Button Back = new Button(this); 
     Back.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     Back.setText("Back"); 

     test2.addView(Back); 

     final LoaderImageView image = new LoaderImageView(this, ImageUrl); 
     image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

我更感興趣的是ScaleType因爲image.ScaleType(FILL)不起作用。

回答

0

你能澄清你想達到什麼嗎。構建與XML完全相同的佈局的Java代碼?如果是這樣,爲什或者,你想在運行時修改XML佈局的某些方面,如果是這樣的話?

可能有更簡單的方法來實現你所需要的。

+0

是的正是這個。構造相同佈局的Java代碼。這是因爲我在從應用程序加載任何圖像之前在應用程序中添加了加載程序,到目前爲止,我只能通過動態設置xml來完成此工作。因爲它只有一個xml我認爲更容易使java代碼給我與我想要的相同的佈局,因爲有一個靜態的xml。 – ghostrider 2012-01-13 00:26:23